标题似乎在描述我的问题方面做得很差;让我试着用一个简化的例子来解释它:
假设我有下表:
_______________________
|id|variant_id|attr_id|
|__|__________|_______|
|1 |15 |110 |
|2 |15 |110 |
|3 |20 |152 |
|4 |20 |110 |
|5 |21 |110 |
|__|__________|_______|
Run Code Online (Sandbox Code Playgroud)
现在,我想要的是一个查询,它选择variant_id和attr_id列的组合出现不止一次的所有行。基本上在这个例子中,它应该选择第 1 行和第 2 行,因为它们的组合variant_id和attr_id出现在表中不止一次。
那可能吗?试图想出一个可能的解决方案,我的头很痛。
小智 5
SELECT variant_id, attr_id
FROM YouTable
GROUP BY variant_id, attr_id
HAVING COUNT(*) > 1
Run Code Online (Sandbox Code Playgroud)
尝试这个查询
SELECT a.* FROM
tbl a
inner join
tbl b
ON a.variant_id =b.variant_id AND a.attr_id = b.attr_id
WHERE a.id <> b.id;
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助