MySQL select specific date from datetime - mysql

I'm trying to setup a mysql select query based on a given date in a datetime column. Something like:
SELECT DATE_FORMAT(colName, '%Y-%m-%d') WHERE..
But I'm unsure on how to do this correctly. I'm using php to do this.

Following example explain the query to select date between 01-Jan-2014 and 15-Jan-2014.
SELECT
DATE_FORMAT(colName, '%Y-%m-%d') AS colName
FROM
`my_table`
WHERE
DATE_FORMAT(colName, '%Y-%m-%d') >= '2014-01-01' AND DATE_FORMAT(colName, '%Y-%m-%d') <= '2014-01-15'

To verify your query, use sqlfiddle and use a query without any table such as
SELECT DATE_FORMAT(now(), '%Y-%m-%d') from dual

Related

MySQL Performance DATE_FORMAT() vs YEAR() AND MONTH()

Which is better for performance when looking for timestamps in current month and year?
SELECT *
FROM mytable
WHERE YEAR(CURRENT_TIMESTAMP) = YEAR(mytable.timestamp)
AND MONTH(CURRENT_TIMESTAMP) = MONTH(mytable.timestamp)
OR
SELECT *
FROM mytable
WHERE DATE_FORMAT(CURRENT_TIMESTAMP,'%m-%Y') = DATE_FORMAT(mytable.timestamp,'%m-%Y')
For better perfomance and able to use index in mytable.timestamp, truncate the current date to month.
SELECT DATE_FORMAT(CURRENT_TIMESTAMP, '%Y-%m-01')
This create a constant value and you can index search for it.
And then you can get all the record from this month
SELECT *
FROM mytable
WHERE mytable.timestamp >= DATE_FORMAT(CURRENT_TIMESTAMP, '%Y-%m-01')

How to select year in sql statement

My code:
$results = $GLOBALS['wpdb']->get_results( 'SELECT * FROM myTable WHERE date = 2014 ORDER BY id DESC', object );
The problem is date is stored in this format: 2014-01-01
So how do I select just the year ( I don't care about month and day for the time being ).
Thanks
Use the year() function:
WHERE year(date) = 2014
or use explicit comparisons:
WHERE (date >= '2014-01-01' and date < '2015-01-01')
The latter is better because it can make use of an index on the date column.
Try this Query :
SELECT * FROM myTable WHERE year(`date`)='2014' ORDER BY id DESC
Try this:
SELECT * FROM myTable WHERE date >= '2014-01-01 00:00:00' ORDER BY id DESC
To select all rows where the year of a date column (called date_col) is equal to 2014 use the year function:
SELECT * FROM `tbl` WHERE Year(`date_col`) = '2014';
You can select year for get posts with query_posts(); parameter is year. Example: query_posts("year=2014"); This is not full question for you, only alternative..

How to convert a mysql datetime column to just date?

Here is an explanation of how my table is set up (in sqlfiddle)
http://sqlfiddle.com/#!2/86a54d/2
Basically, I have two columns. One is a varchar and contains a name. The other is a datetime and contains a certain date with a time.
I want to select rows where the date in the datetime matches today's date. I have tried things like...
select * from alerts_to_send where date_to_send = now()
select * from alerts_to_send where date_to_send = curdate()
I'm not exactly where to go from here... Any ideas would be greatly appreciated :)
Just to re-clarify, I want to select the rows where today's date matches the date part of my datetime type column.
try this
select * from alerts_to_send
where DATE_FORMAT(date_to_send,'%m-%d-%Y') = DATE_FORMAT(now(),'%m-%d-%Y')
fiddle
You can use the date_format function for mysql
select * from alerts_to_send where date_format(date_to_send, "%m/%d/%Y") = date_format(now(), "%m/%d/%Y")
Here is reference to date_format: http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format
An alternative to date_format function you can convert the datetime to date:
select * from alerts_to_send where cast(date_to_send as date) = curdate()
It could be even shorter:
select * from alerts_to_send where date(date_to_send) = date(now())
But you will have problem with using index on it so better will be:
select * from alerts_to_send where date_to_send >= date(now()) and date_to_send < date(now()) + INTERVAL 1 DAY

Mysql - Group by month with Y-m-d format

If I have column 'date' in format 'Y-m-d', can I use GROUP BY based on date column but group by months?
Use MySQL's DATE_FORMAT() function to group only on the desired parts:
GROUP BY DATE_FORMAT(my_column, '%Y-%m')
For Example: created_at is the column you need
SELECT DATE_FORMAT(`created_at`, '%Y-%m-%d')
FROM `table`
GROUP BY DATE_FORMAT(`created_at`, '%Y-%m-%d')
Note that you will need to select created_at.

Optimize sql query - select in select

Which is the best method for optimizing the select in select mysql query ?
This is my example:
SELECT count(distinct email)
FROM emails_stats
WHERE DATE_FORMAT(time, '%Y-%m-%d') >= '2012-12-12'
and email in (SELECT email
FROM `reminder`
WHERE DATE_FORMAT(time, '%Y-%m-%d') = '2012-12-12')
My database has over 500k entries.
SELECT count(distinct emails_stats.email)
FROM emails_stats
JOIN reminder ON emails_stats.email= reminder.email
WHERE
emails_stats.time >= CAST('2012-12-12 00:00:00' AS datetime) AND
(reminder.time BETWEEN CAST('2012-12-12 00:00:00' AS datetime) AND CAST('2012-12-12 23:59:59' AS datetime));
If you use date_format() with the table fields, mysql will need to go through each row in the table, because it needs to get the result of that date_format() function to be able to compare the value with your given string. To make it faster, create an index for the 'time' fields and use this query instead. That way mysql can determine which rows it needs just by looking up the index.
Use an exists clause instead:
SELECT count(distinct email)
FROM emails_stats
WHERE DATE_FORMAT(time, '%Y-%m-%d') >= '2012-12-12'
and exists (SELECT 1
FROM `reminder`
WHERE emails_stats.email = `reminder`.email
and DATE_FORMAT(time, '%Y-%m-%d') = '2012-12-12')
the best method is to use join here like
SELECT count(distinct emails_stats.email)
FROM emails_stats
JOIN reminder ON emails_stats.email= reminder.email
WHERE DATE_FORMAT(emails_stats.time, '%Y-%m-%d') >= '2012-12-12';