0

好的,我有这个代码:

SELECT 
    COUNT(a.auction_id) AS amount, a.owner_id
FROM 
    auctions AS a
    INNER JOIN users AS u ON a.owner_id=u.user_id
    LEFT JOIN winners AS w ON a.auction_id=w.auction_id
WHERE 
    FROM_UNIXTIME(a.start_time) BETWEEN '2013-07-01 00:00:00' AND '2013-07-31 23:59:59' 
AND 
    a.owner_id IN (100012,100027,100030,100063,100065)
AND 
    a.active=1 
AND 
    a.approved=1 
AND 
    a.is_relisted_item=0 
AND 
    a.is_draft=0    
GROUP BY 
    a.owner_id
HAVING 
    amount > 4;

我想做的是像下面这样,但我只是不知道如何写它..

如果a.list_in = 'store' AND a.closed=1那么它必须出现在获胜者表中才能包含在结果集中。所以基本上w.winner_id不应该在winners表中为 NULL 以便将其包含在结果中。

所以基本上,如果a.list_in = 'store'这很好并且应该被包括在内,但是如果a.list_in = 'store' AND a.closed=1它被包括在内w.winner_id就不能为NULL。

我怎样才能做到这一点?

4

1 回答 1

1
    SELECT 
    COUNT(a.auction_id) AS amount, a.owner_id
FROM 
    auctions AS a
    INNER JOIN users AS u ON a.owner_id=u.user_id
    LEFT JOIN winners AS w ON a.auction_id=w.auction_id
WHERE 
    FROM_UNIXTIME(a.start_time) BETWEEN '2013-07-01 00:00:00' AND '2013-07-31 23:59:59' 
AND 
    a.owner_id IN (100012,100027,100030,100063,100065)
AND 
    a.active=1 
AND 
    a.approved=1 
AND 
    a.is_relisted_item=0 
AND 
    a.is_draft=0 
AND (NOT(a.list_in = 'store' AND a.closed=1 ) OR (w.auction_id IS NOT NULL))   
GROUP BY 
    a.owner_id
HAVING 
    amount > 4;

这应该工作

在 where 添加了一个额外的条件

AND (NOT(a.list_in = 'store' AND a.closed=1 ) OR (w.auction_id IS NOT NULL))   
于 2013-08-01T09:21:19.593 回答