SQL显示一列的两个值(如果它们在另一列中共享一个值)

时间:2018-07-18 15:02:18

标签: mysql sql mysql-workbench

我需要原始值> 0的数据,而attributeid = 5和attribute = 197我可以得到它,如果它是attributeid = 5或attributeid = 197,但是我得到2个driveid,如果我按驱动器ID分组我会错过关于信息

示例:

SELECT rawvalue, driveid, attributeid 
from labtech.smartattributes 
where rawvalue>0 and attributeid = 197 or rawvalue>0 and attributeid= 198

# rawvalue, driveid, attributeid
'1', '186', '197'
'8', '488', '198'
'8', '489', '198'
'8', '919', '197'
'8', '919', '198'
'1', '1000', '197'
'3', '1129', '197'
'3', '1129', '198'
'1', '1335', '197'
'1', '1335', '198'

如您所见,在driveid端我有多余的信息

所以,如果我按我分组:

# rawvalue, driveid, attributeid
'1', '186', '197'
'8', '919', '197'
'1', '1000', '197'
'3', '1129', '197'
'1', '1335', '197'
'37', '1389', '197'
'1', '1545', '197'
'6', '2228', '197'
'8', '2390', '197'
'8', '2391', '197'
'8', '2392', '197'
'1', '3068', '197'
'1', '3069', '197'
'39', '3402', '197'
'100663551', '3441', '19'
'1', '3665', '197'

但是,我再也看不到两个属性是否存在

最终目标: 如果两个属性都存在(5和197),则只需要显示即可,但是我不希望显示冗余的driveid值 这可能吗?

链接到数据库:https://www.db-fiddle.com/f/nd7jrmTb7MbypE43zrfBKj/0#&togetherjs=4s61q2QptF

我找到了想要的东西

**潜在解决方案:**

从labtech.smart中选择GROUP_CONCAT(原始值),驱动器ID,GROUP_CONCAT(属性ID) 其中rawvalue> 0和attributeid在('5','198')中,或者rawvalue> 0和attributeid在('5','187')中,或者rawvalue> 0和attributeid在('5','188')中 或rawvalue> 0和attributeid在('5','197')中或rawvalue> 0和attributeid在('187','188')中 或rawvalue> 0和attributeid在('187','197')中或rawvalue> 0和attributeid在('187','198')中 或rawvalue> 0且在('188','197')中的attributeid或rawvalue> 0且在('188','198')中的attributeid 或rawvalue> 0并在('197','198')中设置attributeid 按驱动器ID分组 拥有COUNT(*)> = 3

****我添加了更多内容,因为我希望包含一定数量的属性

输出:

GROUP_CONCAT(原始值),驱动器ID,GROUP_CONCAT(属性ID)

10,7,7,4583,188,197,198

10,8,8,6101,188,197,198

100,1,8,11311,187,188,197

100,1,8,11312,187,188,197

12,9,11,12976,5,187,198

131072,2162716,8,2390,187,188,197

131072,2162716,8,2391,187,188,197

131072,2162716,8,2392,187,188,197

131078,851978,3,6549,5,187,188

131078,9175040,2,6471,5,187,188

176,3,8,488,5,188,198

176,3,8,489,5,188,198

179,71,8,8,5134,187,188,197,198

21368000,2162776,62062592,24,4733,5,187,188,197

2809,239,3,3,1129,5,188,197,198

31195657,27459700,2031641,16,6473,5,187,188,197

314,804,11,23905,5,197,198

32505856,1966569,6,21160,187,188,197

32505856,1966569,6,21161,187,188,197

327685,27,145179,23641,5,187,188

327685,27,145179,23642,5,187,188

3316,1,1,23604,187,197,198

3322,1,1,1335,187,197,198

4,165,22,3274,5,187,188

4,165,22,3275,5,187,188

4,165,22,3281,5,187,188

45,231,1,17217,5,187,198

49676288,1966628,6,21159,187,188,197

49676288,1966628,6,21188,187,188,197

49676288,1966628,6,21189,187,188,197

636,1,1703976,19,19,23781,5,187,188,197,198

65544,196608,27,3104,5,187,188

65544,196608,27,3105,5,187,188

65544,196608,27,3106,5,187,188

65552,29491200,786432,128,19260,5,187,188,197

65552,29491200,786432,128,19262,5,187,188,197

65552,29491200,786432,128,19264,5,187,188,197

72,1,8,23583,187,188,197

774,169,1,1,3998,187,188,197,198

786448,236,240,12743,5,197,198

8,34608,327685,8,8,919,5,187,188,197,198

8,374,1,12266,5,187,198

链接到相关帖子:SELECTING with multiple WHERE conditions on same column

0 个答案:

没有答案