制作一个sql select语句过滤年份

时间:2017-08-08 11:52:39

标签: mysql

我有一个像这样的SQL select语句:

select * from orders where user_id = '1578' and delivery_fee = '2000' 

我想只考虑今年(2017年)的日期,我有一个名为“created_at”的变量,它有像这样的变量,例如

2017-02-0312:00:00

select * from orders where user_id = '1578' and delivery_fee = '2000' and created_at LIKE 'date(Y)%'

但它不起作用,任何人都可以帮忙吗?

2 个答案:

答案 0 :(得分:1)

你可以这样:

select * from orders where user_id = '1578' and delivery_fee = '2000' and created_at > '2017-01-01 21:00:00'

将返回用户1578和delivery_fee = 2000的订单,其中订单是在2017年之后创建的

答案 1 :(得分:1)

请你试试这个:

 FROM sales_flat_order_grid sfog 
 JOIN sales_flat_order_status_history sfosh 
   ON sfosh.parent_id = sfog.entity_id
 JOIN marketplace_orders mo
   ON mo.order_id = sfog.entity_id
 JOIN customer_entity_varchar cev1
   ON mo.seller_id = cev1.entity_id
 JOIN customer_entity_varchar cev2 
   ON mo.seller_id = cev2.entity_id
 JOIN sales_flat_order_address sfoa
   ON sfoa.parent_id = sfog.entity_id
 JOIN sales_flat_order_item sfoi
   ON sfoi.order_id = sfog.entity_id 
 JOIN sales_flat_order sfo
   ON sfo.entity_id = sfog.entity_id
 JOIN marketplace_saleslist ms
   ON ms.mageorderid = sfog.entity_id
WHERE sfog.status IN ('delivered','return','rtndelivered','closed','CanceledBS') 
  AND cev1.attribute_id = '5' 
  AND cev2.attribute_id = '7' 
GROUP BY sfog.entity_id;

要查看的想法和资源:sql get result of current year only

更新

我假设created_at是TIMESSTAMP或DATETIME

    SELECT * FROM orders WHERE user_id = '1578' AND delivery_fee = '2000' AND YEAR(`created_at`) = 2017

供参考,请参阅此处的对话:Refer to this link