在Where子句中使用子查询

时间:2013-10-28 17:11:38

标签: mysql logic subquery

我提交了类似的问题。 显示在三个月中的第一个中至少购买一次并且在第二个或第三个中至少有一个订单的所有客户的客户ID和名称。

不确定这是否正确:

set @rundate = current_date;
set @Mnth_1 = DATE_SUB(@rundate,  INTERVAL 6 MONTH);
set @Mnth_2 = DATE_SUB(@rundate,  INTERVAL 5 MONTH);
set @Mnth_3 = DATE_SUB(@rundate,  INTERVAL 4 MONTH);
Select c_id, c_name
From a_bkorders.bkv_CustOrders
Where Date_format(order_date, '%M %Y') = Date_format(@Mnth_1, '%M %Y')
and order_id in (
     Select order_id
     From a_bkorders.bkv_CustOrders
     Where Date_format(order_date, '%M %Y') = Date_format(@Mnth_2, '%M %Y')
     or Date_format(order_date, '%M %Y') = Date_format(@Mnth_3, '%M %Y'))\G

0 个答案:

没有答案
相关问题