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..
Related
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')
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
I have a database like this:
ID->INT
Date->DateTime
Now i want to write a SELECT to fetch all rows where Date column between "2008/1/1" and "2010/1/1".
SELECT * FROM mytable WHERE `Date` BETWEEN '2008-01-01' and '2010-01-01';
You just apply a filter in a WHERE clause
select *
from yourtable
where date >= '2008-01-01'
and date <= '2010-01-01'
In MySQL I have this query
SELECT DISTINCT date, descr FROM book ORDER BY date
Date is in format yyyy-mm-dd
I want to select only the the books from January 2012. I have tried to use like but that does not work.
Any ideas?
Using DATE_FORMAT function
SELECT DISTINCT date, descr FROM book
WHERE DATE_FORMAT(date, '%Y %m') = DATE_FORMAT('2012-01-01', '%Y %m')
ORDER BY date
Or using MONTH and YEAR functions
SELECT DISTINCT date, descr FROM book
WHERE Month(date) = Month('2012-01-01')
AND Year(date) = Year('2012-01-01')
ORDER BY date;
Or using BETWEEN functions
SELECT DISTINCT date, descr FROM book
WHERE date BETWEEN '2012-01-01'
AND '2012-01-31'
ORDER BY date;
Or using <= and >= operators
SELECT DISTINCT date, descr FROM book
WHERE date >= '2012-01-01'
AND date <= '2012-01-31'
ORDER BY date;
See this SQLFiddle
You can use >= and <= operators here. Check the below code:
SELECT *
FROM book
WHERE date >= '2012-01-01' AND date <= '2012-01-31'
Try this:
SELECT DISTINCT date, descr FROM book WHERE YEAR(date) = '2012' and MONTH(date) = '1'
This works if your "date"-column is a MySQL date field.
If you are adamant that you want to use the LIKE syntax, you can convert the date to CHAR first:
SELECT DISTINCT date, descr FROM book WHERE CAST(date AS char) LIKE '2012-01%' ORDER BY date;
Using like also works. With #hims056 fiddle, you can test it:
SELECT DISTINCT ID, date FROM book
WHERE date LIKE '2012-01%'
ORDER BY date;
However, it's not usual to use a like for date filtering, for me it's more natural to use >= and <= , or between. Also, there's a performance benefit.
SELECT DISTINCT date, descr
FROM book
WHERE YEAR = DATE(NOW()) AND MONTH(date) = '1'
This will give you this years books
I have a table with a column date (type mysql date : yyyy-mm-dd)
I have done this request:
Select Month(MAX(date)) AS last_month, YEAR(MAX(date)) AS last_year FROM mytable
to get the last year and last month.
I want for this last year and last month select all data in one request.
How can I use something like:
Select * from mytable where date like 'last_year-last_month-%'
this in sub-query ??
Is the table being update continuously?
If so, then you can go without the subqueries since they will slow the query down by quite a lot.
This selects everything from the current month.
SELECT * FROM mytable
WHERE MONTH(date) = MONTH(NOW())
AND YEAR(date) = YEAR(NOW())
ORDER BY date ASC
And this one selects everything from the current and last year.
SELECT * FROM mytable
WHERE YEAR(date) > YEAR(NOW())-1
ORDER BY date ASC
This should do the trick (maybe not the best performing solution though):
SELECT * FROM mytable
WHERE MONTH(date) = (SELECT MONTH(MAX(date) FROM mytable))
AND YEAR(date) = (SELECT YEAR(MAX(date) FROM mytable));
EDIT: Corrected brackets.
Thanks for alls,
here is the good request :
SELECT * FROM mytable
WHERE MONTH(date) = (select Month(MAX(date)) FROM mytable)
AND YEAR(date) = (select YEAR(MAX(date)) FROM mytable)
ORDER BY date ASC