0

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" ;-)

2 Answers 2

2

You have to GROUP BY the date part of the datetime field:

SELECT DATE(`datetime`), COUNT(*)
FROM mytable
WHERE Code = 'BAR'
GROUP BY DATE(`datetime`)
Sign up to request clarification or add additional context in comments.

Comments

1

it will work for you

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

1 Comment

You need to trim off the time part to get grouping by date.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.