0

你看,t1 和 t2 有一些相似的行,在第一个查询中我选择了不匹配的行,我使用了以下查询

SELECT DISTINCT t1.usr_id
FROM t1, t2
WHERE t1.usr_id != t2.usr_id
AND t1.event =  '$event'
AND t1.client =  '$client'
GROUP BY t1.usr_id

现在我想使用上面查询的结果从匹配 usr_id 的 t3 中选择 *。我已经尝试过其他几乎类似问题的答案,但没有一个是从一个查询中减去并使用结果从第二个查询中获取数据

4

1 回答 1

1

尝试这个:

SELECT t3.usr_id FROM t3 WHERE t3.usr_id IN 
(SELECT DISTINCT t1.usr_id
FROM t1, t2
WHERE t1.usr_id != t2.usr_id
AND t1.event =  '$event'
AND t1.client =  '$client'
GROUP BY t1.usr_id)

但它可能比:

SELECT table.usr_id
FROM ( SELECT DISTINCT t1.usr_id
FROM t1, t2
WHERE t1.usr_id != t2.usr_id
AND t1.event =  '$event'
AND t1.client =  '$client'
GROUP BY t1.usr_id ) as table
JOIN t3 ON t3.usr_id = table.usr_id 
于 2012-07-24T19:04:03.987 回答