具有重复表名的SELECT语句

时间:2018-02-27 19:48:39

标签: mysql select duplicates

我有两个不同的功能sql语句需要组合。

尝试避免在SELECT语句中重复名称,同时仍然检索我在SQL查询中查找的数据。

第一个更长的查询:

Select USER.USER_NAME, ASSET.NAME AS KACE_ASSET_NAME, FIELD_31, V3.FIELD_VALUE AS CUSTOM_3, FIELD_20 AS BARCODE, FIELD_27 AS SERIAL_NUMBER

FROM ASSET
JOIN USER ON ASSET.OWNER_ID = USER.ID

JOIN ASSET_DATA_5 ON ASSET.ASSET_DATA_ID = ASSET_DATA_5.ID AND ASSET_TYPE_ID = 5


LEFT JOIN USER_FIELD_VALUE V3 ON V3.USER_ID = USER.ID AND V3.FIELD_ID = 3 

LEFT JOIN ASSET ASSET_LOCATION  ON ASSET_LOCATION.ID = ASSET.LOCATION_ID
LEFT JOIN ASSET_ASSOCIATION J21 ON J21.ASSET_ID = ASSET.ID AND J21.ASSET_FIELD_ID=21
LEFT JOIN ASSET A21 ON A21.ID = J21.ASSOCIATED_ASSET_ID
LEFT JOIN ASSET_DATA_1 AD21 ON AD21.ID = A21.ASSET_DATA_ID
WHERE V3.FIELD_VALUE = 'TERMINATED'

第二个较小的查询:

SELECT ASSET.NAME AS CITY 
FROM ASSET_DATA_1 LEFT JOIN ASSET ON ASSET_DATA_1.ID = ASSET.ASSET_DATA_ID AND ASSET.ASSET_TYPE_ID=1

我尽最大努力将两者结合起来:

Select USER.USER_NAME, ASSET.NAME AS KACE_ASSET_NAME, FIELD_31, V3.FIELD_VALUE AS CUSTOM_3, FIELD_20 AS BARCODE, FIELD_27 AS SERIAL_NUMBER, ASSET.NAME as CITY

FROM ASSET
JOIN USER ON ASSET.OWNER_ID = USER.ID

JOIN ASSET_DATA_5 ON ASSET.ASSET_DATA_ID = ASSET_DATA_5.ID AND ASSET_TYPE_ID = 5


LEFT JOIN USER_FIELD_VALUE V3 ON V3.USER_ID = USER.ID AND V3.FIELD_ID = 3 

LEFT JOIN ASSET ASSET_LOCATION  ON ASSET_LOCATION.ID = ASSET.LOCATION_ID
LEFT JOIN ASSET_ASSOCIATION J21 ON J21.ASSET_ID = ASSET.ID AND J21.ASSET_FIELD_ID=21
LEFT JOIN ASSET A21 ON A21.ID = J21.ASSOCIATED_ASSET_ID
LEFT JOIN ASSET_DATA_1 AD21 ON AD21.ID = A21.ASSET_DATA_ID


#IMPORTANT JOIN HERE - NEED TO LINK THIS INFO BACK TO SELECT STATEMENT
LEFT JOIN ASSET ASSET_DATA_1 ON ASSET_DATA_1.ID = ASSET.ASSET_DATA_ID AND ASSET.ASSET_TYPE_ID=1  


WHERE V3.FIELD_VALUE = 'TERMINATED'

因此尝试构造此SELECT语句,以便将asset.name正确链接回此处列出的最后一个LEFT JOIN。问题是,我相信有两列名为ASSET.NAME和两个ASSET表。我不确定如何致电" ASSET.NAME"来自" ASSET_DATA_1"的数据SELECT语句中的表,而不只是获取第一个Asset表的副本。 我已经四处寻找答案,并在括号和UNION命令中尝试过SELECT语句,但这些都不是答案。 SQL noob,感觉有点不知所措。

3 个答案:

答案 0 :(得分:0)

要删除重复项,请在所有选定列上使用group by。

SELECT USER.USER_NAME, ASSET.NAME AS KACE_ASSET_NAME, FIELD_31, V3.FIELD_VALUE AS CUSTOM_3, FIELD_20 AS BARCODE, FIELD_27 AS SERIAL_NUMBER
FROM ASSET
JOIN USER ON ASSET.OWNER_ID = USER.ID
JOIN ASSET_DATA_5 ON ASSET.ASSET_DATA_ID = ASSET_DATA_5.ID AND ASSET_TYPE_ID = 5
LEFT JOIN USER_FIELD_VALUE V3 ON V3.USER_ID = USER.ID AND V3.FIELD_ID = 3 
LEFT JOIN ASSET ASSET_LOCATION  ON ASSET_LOCATION.ID = ASSET.LOCATION_ID
LEFT JOIN ASSET_ASSOCIATION J21 ON J21.ASSET_ID = ASSET.ID AND J21.ASSET_FIELD_ID=21
LEFT JOIN ASSET A21 ON A21.ID = J21.ASSOCIATED_ASSET_ID
LEFT JOIN ASSET_DATA_1 AD21 ON AD21.ID = A21.ASSET_DATA_ID
WHERE V3.FIELD_VALUE = 'TERMINATED'
GROUP BY USER.USER_NAME, ASSET.NAME, FIELD_31, V3.FIELD_VALUE, FIELD_20, FIELD_27;

答案 1 :(得分:0)

如果要删除重复的行,则应使用DISTINCT子句

Select DISTINCT 

    USER.USER_NAME, ASSET.NAME AS KACE_ASSET_NAME, FIELD_31,
     V3.FIELD_VALUE AS CUSTOM_3, FIELD_20 AS BARCODE, FIELD_27 AS SERIAL_NUMBER

FROM ASSET
JOIN USER ON ASSET.OWNER_ID = USER.ID

JOIN ASSET_DATA_5 ON ASSET.ASSET_DATA_ID = ASSET_DATA_5.ID AND ASSET_TYPE_ID = 5


LEFT JOIN USER_FIELD_VALUE V3 ON V3.USER_ID = USER.ID AND V3.FIELD_ID = 3 

LEFT JOIN ASSET ASSET_LOCATION  ON ASSET_LOCATION.ID = ASSET.LOCATION_ID
LEFT JOIN ASSET_ASSOCIATION J21 ON J21.ASSET_ID = ASSET.ID AND J21.ASSET_FIELD_ID=21
LEFT JOIN ASSET A21 ON A21.ID = J21.ASSOCIATED_ASSET_ID
LEFT JOIN ASSET_DATA_1 AD21 ON AD21.ID = A21.ASSET_DATA_ID
WHERE V3.FIELD_VALUE = 'TERMINATED'

(在没有聚合函数的情况下,对于此作用域使用group by在sql中不推荐使用,并且不允许使用最新版本的mysql)

答案 2 :(得分:0)

您可以为资产表的第二个副本添加其他别名:

Select USER.USER_NAME, 
ASSET.NAME AS KACE_ASSET_NAME, 
FIELD_31, 
V3.FIELD_VALUE AS CUSTOM_3, 
FIELD_20 AS BARCODE, 
FIELD_27 AS SERIAL_NUMBER,
 asset_again .NAME as CITY

FROM ASSET
JOIN USER ON ASSET.OWNER_ID = USER.ID

JOIN ASSET_DATA_5 ON ASSET.ASSET_DATA_ID = ASSET_DATA_5.ID AND ASSET_TYPE_ID = 5


LEFT JOIN USER_FIELD_VALUE V3 ON V3.USER_ID = USER.ID AND V3.FIELD_ID = 3 

LEFT JOIN ASSET ASSET_LOCATION  ON ASSET_LOCATION.ID = ASSET.LOCATION_ID
LEFT JOIN ASSET_ASSOCIATION J21 ON J21.ASSET_ID = ASSET.ID AND J21.ASSET_FIELD_ID=21
LEFT JOIN ASSET A21 ON A21.ID = J21.ASSOCIATED_ASSET_ID
LEFT JOIN ASSET_DATA_1 AD21 ON AD21.ID = A21.ASSET_DATA_ID


#IMPORTANT JOIN HERE - NEED TO LINK THIS INFO BACK TO SELECT STATEMENT
LEFT JOIN ASSET asset_again ON ad21 .ID = asset_again.ASSET_DATA_ID AND asset_again .ASSET_TYPE_ID=1  


WHERE V3.FIELD_VALUE = 'TERMINATED'

事实上,再次阅读这个问题,我看到你试图通过使用ASSET_DATA_1作为别名来混淆事物。使用唯一(对查询)别名并使用该别名来引用列列表中的表