2013-12-09 73 views
0

我正在运行一个SQL查询,该查询显示凭证集合,按照凭单在另一个数据库中使用的次数为0的顺序排列。将用户状态作为SQL查询的一部分进行检查

虽然最后一步,我想检查相应的'voucher_credits.user_id'的'user.state'仍然'活跃',我该如何加入?

SELECT vouchers.code, count(voucher_credits.voucher_id) as Activated 
    FROM vouchers, voucher_credits, users 
    WHERE vouchers.code LIKE '%sentme%' 
     AND vouchers.id = voucher_credits.voucher_id 
     AND voucher_credits.remaining_credit = 0 
     AND *** the user.state of voucher_credits.user is 'active' *** 
    GROUP by vouchers.code 
    ORDER by count(voucher_credits.voucher_id) DESC 
    LIMIT 50 
+0

请使用ANSI连接语法('INNER JOIN')。它会使事情模糊可读。 –

回答

0

尝试类似,

SELECT vouchers.code, count(voucher_credits.voucher_id) as Activated 
FROM vouchers, voucher_credits, users 
WHERE vouchers.code LIKE '%sentme%' 
    AND vouchers.id = voucher_credits.voucher_id 
    AND voucher_credits.remaining_credit = 0 
    AND users.id = voucher_credits.user_id AND user.state = 'active' 
GROUP by vouchers.code 
ORDER by count(voucher_credits.voucher_id) DESC 
LIMIT 50