473,387 Members | 1,650 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

MySQL MAX and COUNT together

Hi experts,

I have 2 tables which goes as follows:

tbl_item: {id, name, etc}
tbl_votes: {id, item_id}

Therefore each item has a certain number of votes. I want to be able to extract the item with the most number of votes. However there might be a case where top votes are at tie with each other, so 2 or more items have to be selected. Currently my sql is as follows:
Expand|Select|Wrap|Line Numbers
  1. SELECT
  2. (SELECT max(num_votes) from
  3. (SELECT count(v.item_id) AS num_votes
  4. FROM tbl_item AS i
  5. LEFT JOIN tbl_votes AS v ON v.item_id = i.id
  6. GROUP BY i.id ORDER BY num_votes DESC) as q1)
  7. as maxi,
  8. count(v.item_id) AS num_votes, i.*
  9. FROM tbl_item AS I
  10. LEFT JOIN tbl_votes AS v ON v.item_id = i.id
  11. GROUP BY i.id
  12. having maxi=num_votes
  13.  
This query gives me my desired results, but is there a better way? cos this dont look good at all
Oct 19 '07 #1
1 10144
ronverdonk
4,258 Expert 4TB
Next time enclose any code within the proper code tags. See the Posting Guidelines on how to do that.

moderator
Oct 19 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: knoak | last post by:
Hi there, I have a small question: I have a table with lots of rows in it. Of course all have a different id, but each can be assigned to a certain category. Categories correspond with 1 - 10....
0
by: Bob Bedford | last post by:
I've no access to mysql NG, so I ask here. I've now those 2 queries: select count(IDAnnounce)as countarticles from XXX where DateTimeSell is NULL select count(IDAnnounce)as countarticles from...
0
by: skiserf | last post by:
Hi, I am having this interesting SQL problem with mysql Ver 12.22 Distrib 4.0.16, for apple-darwin7.0 (powerpc). For some reason count(*) does give the right result, when I replace it with...
3
by: auron | last post by:
Hi there, I have a really stupid and banal problem with showing the results of a MySQL query in PHP, preciselly with MySQL count() function that gives to a variable in PHP the result. NOTE:...
14
by: dottty | last post by:
Hi, i have a table that has the following fields: id, name, dept, pay 1, John, Sales, 4000 2, Peter, HR, 5000 etc. How do i count how many people there are in each dept with an sql query? ...
3
by: Auddog | last post by:
I have the following query that works in mysql: select id, order_no, price, count(item_no), sum(price) from production WHERE item_no = '27714' group by item_no; When I setup my query in php,...
9
by: Akhenaten | last post by:
The following snippet (for whatever reason) returns no value for the count. Suggestions? $arr = array ("A", "B", "C", "D", "E"); foreach ($arr as $client) { $count = mysql_query('SELECT...
5
by: andrewteg | last post by:
Always finding answers here so finally posting a question! I have a MySQL query and I am trying to get a max count to help replicate an Access Pivot table. Query: SELECT DISTINCT oTech,...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.