在MySQL中,我有此查询
SELECT DISTINCT date, descr FROM book ORDER BY date
日期格式 yyyy-mm-dd
yyyy-mm-dd
我只选择 2012年1月以后 的书籍。我尝试使用,like但这不起作用。
like
有任何想法吗?
使用DATE_FORMAT功能
DATE_FORMAT
SELECT DISTINCT date, descr FROM book WHERE DATE_FORMAT(date, '%Y %m') = DATE_FORMAT('2012-01-01', '%Y %m') ORDER BY date
或使用MONTH和YEAR功能
MONTH
YEAR
SELECT DISTINCT date, descr FROM book WHERE Month(date) = Month('2012-01-01') AND Year(date) = Year('2012-01-01') ORDER BY date;
或使用BETWEEN功能
BETWEEN
SELECT DISTINCT date, descr FROM book WHERE date BETWEEN '2012-01-01' AND '2012-01-31' ORDER BY date;
或使用<=和>=运算符
<=
>=
SELECT DISTINCT date, descr FROM book WHERE date >= '2012-01-01' AND date <= '2012-01-31' ORDER BY date;