This is a discussion on GROUP BY error :( within the MySQL Database forums, part of the Database Forums category; I have 2 tables categories & applications. I am trying to select all the categories and each category count all ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
I have 2 tables categories & applications. I am trying to select all
the categories and each category count all the applications as a total. The link below has the query I used and the query I tried to create to accomplish them, but seems to have no luck. http://dpaste.com/15434/ |
|
|||
|
Sutabi wrote:
> I have 2 tables categories & applications. I am trying to select all > the categories and each category count all the applications as a > total. The link below has the query I used and the query I tried to > create to accomplish them, but seems to have no luck. > > http://dpaste.com/15434/ > First of all, you should put your query, expected result, etc. in the message, not refer to a web page. What happens if next week someone has a similar problem? Your web page will be gone, but your question won't. For the record, the info is: QUERY: ================================================= SELECT c.*, COUNT(DISTINCT a.app_id) AS app_total FROM categories AS c, applications AS a WHERE (a.cat_id = c.cat_id) GROUP BY a.app_id RESULT: ==================================== cat_id |cat_name | app_total ------------------------------------ 1 |News | 1 1 |News | 1 What I was hoping to get: ==================================== cat_id |cat_name | app_total ------------------------------------ 1 |News | 2 So I revamped the query to group by `app_total`, but guess what I CANT!! QUERY: ================================================= SELECT c.*, COUNT(DISTINCT a.app_id) AS app_total FROM categories AS c, applications AS a WHERE (a.cat_id = c.cat_id) GROUP BY app_total Error: ================================================= #1056 - Can't group on 'app_total' You don't want to group by app_total, because this would group everything with "1" for a total together, no matter what the cat_id is. Now - what happens if you GROUP BY c.cat_id? -- ================== Remove the "x" from my email address Jerry Stuckle JDS Computer Training Corp. jstucklex@attglobal.net ================== |