PHP使用相同的值回显id

时间:2015-05-14 16:54:02

标签: php mysql

我有这张桌子

enter image description here

我想要做的是查询/回显那些具有相同值的id,例如id 1,id 3具有相同的值。这可能在同一个表格和列中吗?谢谢!

1 个答案:

答案 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

DEMO