Tuesday 4 September 2018

MySql Query with Select Count and Group By

I'm developing a php page and I should make a MySql query for extract some data from a table.

My table is like this:
Date (datetime) | Code (text)
2016-02-25 11:47:01 | FOO
2016-02-25 17:25:03 | BAR
2016-02-24 10:42:04 | BAR
2016-02-24 19:45:05 | BAR
2016-01-17 13:12:06 | BAR
2016-02-23 22:36:07 | XXX
2016-02-20 03:25:08 | YYY

I would like to write a query to obtain only "BAR" with count of occurrency by day, something like:
2016-02-25 | 1
2016-02-24 | 2
2016-01-17 | 1

The only part of query that I'm able to write is SELECT [...] WHERE Code="BAR" ;-)

it will work for you
    select DATE(`datetime`)as date,count(Date)
    from table_name
WHERE Code = 'BAR'
    group by DATE(`datetime`)

0 comments:

Post a Comment