Mike
Mike

Reputation: 6839

mysql count function problems

I have a table which is formatted as:

id |  food  | userID | beerID

I am trying to select all the foods for a given beerID and a count of repeats. SO if there are 3 entries for pizza I get results like:

food      | beerID | count

pizza     | 34     | 2
hot wings | 34     | 1
pasta     | 34     | 5

Does this work? I am a bit confused on using the count with the group by.

select food, beerID , count() where beerID = 34 group by food 

Upvotes: 0

Views: 44

Answers (2)

Josh from Qaribou
Josh from Qaribou

Reputation: 6898

SELECT food, beerID, count(1) as beerCount
    FROM tablename
    WHERE beerID=34
    GROUP BY food;

Upvotes: 0

Sterling Archer
Sterling Archer

Reputation: 22395

Give the count a parameter.

select food, beerID, count(food) from tablename as num where beerID = 34 group by food;

Footnote: "does this work" is a bad question. You'll 100% chance be rebutted with "did you not try it..?"

As noted in the comments you aren't selecting from a specific table, so it will error out

Upvotes: 2

Related Questions