我有这张桌子
我想要做的是查询/回显那些具有相同值的id,例如id 1,id 3具有相同的值。这可能在同一个表格和列中吗?谢谢!
答案 0 :(得分:2)
您可以使用自联接来获取具有相同权重的所有ID对
SELECT t1.weight, t1.id AS id1, t2.id AS id2
FROM YourTable AS t1
JOIN YourTable AS t2 ON t1.weight = t2.weight AND t1.id < t2.id
或GROUP_CONCAT
在一行中获取具有相同权重的所有ID:
SELECT weight, GROUP_CONCAT(id) AS ids
FROM YourTable
GROUP BY weight
HAVING COUNT(*) > 1
每个体重只需一对:
SELECT t1.weight, t1.id AS id1, t2.id AS id2
FROM YourTable AS t1
JOIN YourTable AS t2 ON t1.weight = t2.weight AND t1.id < t2.id
GROUP BY t1.weight