连接条件不正常

时间:2015-12-29 15:51:25

标签: mysql database

我有两张桌子

customers (id, name....,created_at)
coupons (id, customer_id, ....,created_id)

我正在尝试计算所有优惠券,其中created_at是2015-12-29,而客户的created_at不是2015-12-29

select 
    count(*) as aggregate 
from 
    `coupons` 
where  
    (select count(*) from `customers` 
    where 
        `coupons`.`customer_id` = `customers`.`id` 
    and 
        DATE(`customers`.`created_at`) <> '2015-12-29') >= 1 
and 
    DATE(`updated_at`) = '2015-12-29'

此查询工作正常,但当我再添加一个约束来查询client_id = 1时,它不起作用。

select 
    count(*) as aggregate 
from 
    `coupons` 
where  
    (select count(*) from `customers` 
    where 
        `coupons`.`customer_id` = `customers`.`id` 
    and 
        DATE(`customers`.`created_at`) <> '2015-12-29') >= 1 
and 
    DATE(`updated_at`) = '2015-12-29'
and
    `client_id` = 1

它应返回非零值,但返回0

虚拟数据

客户

id, name,   created_at
1   ehsan   2015-12-29 12:10:10
2   ehs     2015-12-28 12:10:10

优惠券

id, customer_id, updated_at            client_id
1   1            2015-12-29 12:10:10   1
2   2            2015-12-29 12:10:10   1
3   2            2015-12-29 12:10:10   1

0 个答案:

没有答案
相关问题