MySQL将共享给定列值的所有行与共享另一列的行进行比较

时间:2014-01-29 22:59:08

标签: mysql

您好我想要比较value列中包含36列中包含customized_id的所有行以及包含21的所有行的数据。我有兴趣观察这些数据之间的任何差异。谢谢!

| Customized_id  |  value  |
----------------------------
|     36         |  PASS   |
|     36         |  FAIL   |
|     36         |  PASS   |
|     21         |  PASS   |
|     21         |  PASS   |
|     21         |  FAIL   |

使用以下内容我能够返回接近我想要的东西,但行不断重复,我不知道如何修复。一旦我只返回了所需的数据,我想只显示每个customize_id行的“value”列数据(PASS / FAIL)存在差异的行。

SELECT b.customized_id, b.value, c.customized_id, c.value
FROM `redmine`.`custom_values` 
left join `redmine`.`custom_values` as b
      on b.customized_id = '36'
left join `redmine`.`custom_values` as c
      on c.customized_id = '21';



| Customized_id  |  value  | Customized_id   |  value |
------------------------------------------------------------------------------------
|     36         |  PASS   |     21         |  FAIL   |
|     36         |  PASS   |     21         |  FAIL   |
|     36         |  PASS   |     21         |  PASS   |
|     36         |  PASS   |     21         |  PASS   |
|     36         |  PASS   |     21         |  PASS   |
|     36         |  PASS   |     21         |  PASS   |

2 个答案:

答案 0 :(得分:0)

这是你想要的吗?

SELECT Customized_id, COUNT(DISTINCT value) values
FROM YourTable
GROUP BY Customized_id

这将告诉您特定ID的所有值是相同还是不同。

答案 1 :(得分:0)

这是你要找的?

SELECT a.customized_id, a.value
FROM `redmine`.`custom_values` as a
left join `redmine`.`custom_values` as b
      on b.customized_id = 24
left join `redmine`.`custom_values` as c
      on c.customized_id = 36
left join //something else