0

我有以下查询:

SELECT * 
FROM users
WHERE id NOT 
IN (

SELECT user_id
FROM  `bids` 
WHERE DATE_SUB(  DATE_ADD( CURDATE( ) , INTERVAL 7 
DAY ) , INTERVAL 14 
DAY ) <= created
)
AND id NOT 
IN (

SELECT user_id
FROM coupon_used WHERE code = 'ACTNOW'
)
AND id
IN (

SELECT user_id
FROM accounts
)

我只想获取特定用户并搜索他们,而不是搜索表中的所有用户。就像我有 id 为 1,2,3,4,5 的用户列表一样,我只想搜索这些用户

4

2 回答 2

0

只需WHERE使用添加一个子句IN()

SELECT * 
FROM users
WHERE id IN(1,2,3,4,5)
于 2013-03-01T17:00:49.320 回答
0

我相信使用左外连接将简化您的查询并有望提高性能

SELECT users.* 
FROM users
LEFT OUTER JOIN bids on bids.user_id = users.id AND DATE_SUB(DATE_ADD(CURDATE(), INTERVAL 7 DAY), INTERVAL 14 DAY) <= bids.created
LEFT OUTER JOIN coupon_used on coupon_used.user_id = users.id AND coupon_used.code = 'ACTNOW'
INNER JOIN accounts on accounts.user_id = users.id
WHERE bids.id is null AND coupon_used.id is null
    AND users.id in (1,2,3,4,5)
于 2013-03-01T17:53:09.513 回答