在我的表中,我有一个列current_date,其值是2012-11-27。
这是我的代码
$query = QModel::query("SELECT * FROM transaction WHERE current_date='$order_date'"); $order_date的值仅为"2012-10",如何将current_date拆分为仅为“2012-10”的年度月.
我应该使用CHARINDEX来分割current_date吗?怎么请..。
谢谢
发布于 2012-11-27 07:04:34
那么,如果您需要在一个月内选择所有记录,您可以这样做:
SELECT *
FROM table
WHERE current_date >= convert(datetime, '20121001', 112) and current_date < convert(datetime, '20121101', 112)我不能建议使用month和year函数,因为索引不能工作。
更新: AFAIK,在MySQL中
SELECT *
FROM table
WHERE current_date >= str_to_date('20121001', '%y%m%d') and current_date < str_to_date('20121101', '%y%m%d')发布于 2012-11-27 07:04:33
如果您使用的是MySQL
SELECT *
FROM table
WHERE YEAR(current_date) = 2012 AND
MONTH(current_date) = 10对于SQL Server
SELECT *
FROM table
WHERE DATEPART(year,current_date) = 2012 AND
DATEPART(month, current_date) = 10发布于 2012-11-27 07:04:36
试试这个:
适用于mysql和sql server。
SELECT *
FROM table
WHERE month(current_date)=10
and year(current_date)=2012https://stackoverflow.com/questions/13578989
复制相似问题