PHP SQL Count 3 column totals and group by another column

时间:2015-05-24 21:32:03

标签: php mysql sql

I am trying to put together an SQL query where I can count 3 columns and group by another one.

Here is the table:

id thedate cars bikes planes from stats group by thedate

What I'm trying to do is to get the totals for each day of each cars, bikes and planes.

So I've tried:

SELECT id, thedate, cars, bikes, planes from stats GROUP BY thedate

SELECT id, thedate, count(cars), count(bikes), count(planes) from stats GROUP BY thedate

None of them work.

How can I do this?

1 个答案:

答案 0 :(得分:1)

SELECT DATE(thedate), count(cars), count(bikes), count(planes) from stats GROUP BY DATE(thedate)

You should group by date only (without hours and minutes) and shouldn't select an id field.