Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple SELECT COUNT SQL query error

Tags:

sql

mysql

I'm trying to get the total number of rows using this SQL query, the result should be = 1

SELECT COUNT(id) FROM offer_process WHERE uid = 103 AND date = '2014-08-20'

I don't know exactly how to get the total number of rows from a date (2014-08-20) in this case.

enter image description here

like image 508
Alfonso M. García Astorga Avatar asked Sep 13 '26 00:09

Alfonso M. García Astorga


2 Answers

You have a datetime column.
To check only date part, use DATE() MySQL function

SELECT COUNT(id) FROM offer_process WHERE uid = 103 AND DATE(date) = '2014-08-20'

Also, if you have an index on your date field, you should avoid using DATE/TIME MySQL functions. Instead, you can use WHERE date LIKE '2014-08-20%' for better performance

like image 139
Justin Iurman Avatar answered Sep 15 '26 15:09

Justin Iurman


You may try this:

SELECT COUNT(id) FROM offer_process WHERE uid = 103 
AND DATE_FORMAT(`date`, "%Y-%m-%d") = '2014-08-20'

or you may try using date

SELECT COUNT(id) FROM offer_process
WHERE uid = 103 AND date(`date`) = '2014-08-20'
like image 22
Rahul Tripathi Avatar answered Sep 15 '26 14:09

Rahul Tripathi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!