检查用户状态作为SQL查询的一部分

时间:2013-12-09 02:34:50

标签: sql postgresql

我正在运行一个SQL查询,该查询显示了一系列凭证,按照在另一个数据库中将凭证的使用次数降低到0英镑的顺序。

作为最后一步,我想检查相应'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

1 个答案:

答案 0 :(得分: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