SO社区,这是我的SQL查询,运行时没有错误:
SELECT DISTINCT
(L.LOCATION) AS "LOC",
C.CITY_NAME AS "CIT",
H.STREET_NAME AS "STR",
U.APT_NAME AS "APT",
U.NUM AS "NUM",
L.DESCRIPTION AS "DES",
H.POP AS "POP",
S.STATE_CODE AS "STA",
H.SITE_NAME AS "SIT",
U.COUNTY AS "COU",
L.DESCRIPTION AS "PRO"
FROM
LOCATIONS L
LEFT OUTER JOIN CGZIP9 Z
ON L.L_ZIPCODE = Z.ZIPCODE
LEFT OUTER JOIN STREET_V H
ON L.LOCATION = H.LOCATION
LEFT OUTER JOIN CGZIPCITY Y
ON Z.CITYID = Y.CITYID
LEFT OUTER JOIN CGCITY C
ON Y.CITYID = C.CITYID
LEFT OUTER JOIN CGSTATE S
ON C.STATEFIPS = S.STATEFIPS
LEFT OUTER JOIN UNIT U
ON L.UNITID = U.UNIT_ID
WHERE
L.LOCATION IN ('1', '2', '3', '4', '5', '1', '2', '4')
ORDER BY L.LOCATION ASC;
我正在尝试检索IN中所有值的记录(甚至包括重复的内容,例如“ 1”,“ 2”和“ 4”)
这是我运行上述代码时的输出:
=============================================
|LOC|CIT|STR|APT|NUM|DES|POP|STA|SIT|COU|PRO|
=============================================
| 1 | X | X | X | X | X | X | X | X | X | X |
| 2 | X | X | X | X | X | X | X | X | X | X |
| 3 | X | X | X | X | X | X | X | X | X | X |
| 4 | X | X | X | X | X | X | X | X | X | X |
| 5 | X | X | X | X | X | X | X | X | X | X |
=============================================
这是我想要的输出:
=============================================
|LOC|CIT|STR|APT|NUM|DES|POP|STA|SIT|COU|PRO|
=============================================
| 1 | X | X | X | X | X | X | X | X | X | X |
| 2 | X | X | X | X | X | X | X | X | X | X |
| 3 | X | X | X | X | X | X | X | X | X | X |
| 4 | X | X | X | X | X | X | X | X | X | X |
| 5 | X | X | X | X | X | X | X | X | X | X |
| 1 | X | X | X | X | X | X | X | X | X | X |
| 2 | X | X | X | X | X | X | X | X | X | X |
| 4 | X | X | X | X | X | X | X | X | X | X |
=============================================
关于如何实现此目标的任何建议?
答案 0 :(得分:2)
请尝试不过滤重复项的UNION ALL方法,如下所示:
SELECT DISTINCT (L.LOCATION) AS "LOC",
C.CITY_NAME AS "CIT",
H.STREET_NAME AS "STR",
U.APT_NAME AS "APT",
U.NUM AS "NUM",
L.DESCRIPTION AS "DES",
H.POP AS "POP",
S.STATE_CODE AS "STA",
H.SITE_NAME AS "SIT",
U.COUNTY AS "COU",
L.DESCRIPTION AS "PRO"
FROM LOCATIONS L
LEFT OUTER JOIN CGZIP9 Z ON L.L_ZIPCODE = Z.ZIPCODE
LEFT OUTER JOIN STREET_V H ON L.LOCATION = H.LOCATION
LEFT OUTER JOIN CGZIPCITY Y ON Z.CITYID = Y.CITYID
LEFT OUTER JOIN CGCITY C ON Y.CITYID = C.CITYID
LEFT OUTER JOIN CGSTATE S ON C.STATEFIPS = S.STATEFIPS
LEFT OUTER JOIN UNIT U ON L.UNITID = U.UNIT_ID
WHERE L.LOCATION IN ('1',
'2',
'3',
'4',
'5')
UNION ALL
SELECT DISTINCT (L.LOCATION) AS "LOC",
C.CITY_NAME AS "CIT",
H.STREET_NAME AS "STR",
U.APT_NAME AS "APT",
U.NUM AS "NUM",
L.DESCRIPTION AS "DES",
H.POP AS "POP",
S.STATE_CODE AS "STA",
H.SITE_NAME AS "SIT",
U.COUNTY AS "COU",
L.DESCRIPTION AS "PRO"
FROM LOCATIONS L
LEFT OUTER JOIN CGZIP9 Z ON L.L_ZIPCODE = Z.ZIPCODE
LEFT OUTER JOIN STREET_V H ON L.LOCATION = H.LOCATION
LEFT OUTER JOIN CGZIPCITY Y ON Z.CITYID = Y.CITYID
LEFT OUTER JOIN CGCITY C ON Y.CITYID = C.CITYID
LEFT OUTER JOIN CGSTATE S ON C.STATEFIPS = S.STATEFIPS
LEFT OUTER JOIN UNIT U ON L.UNITID = U.UNIT_ID
WHERE L.LOCATION IN ('1', '2', '4')
ORDER BY L.LOCATION ASC;
答案 1 :(得分:0)
这将起作用:
SELECT
L.LOCATION AS "LOC",
C.CITY_NAME AS "CIT",
H.STREET_NAME AS "STR",
U.APT_NAME AS "APT",
U.NUM AS "NUM",
L.DESCRIPTION AS "DES",
H.POP AS "POP",
S.STATE_CODE AS "STA",
H.SITE_NAME AS "SIT",
U.COUNTY AS "COU",
L.DESCRIPTION AS "PRO"
FROM
LOCATIONS L
LEFT OUTER JOIN CGZIP9 Z
ON L.L_ZIPCODE = Z.ZIPCODE
LEFT OUTER JOIN STREET_V H
ON L.LOCATION = H.LOCATION
LEFT OUTER JOIN CGZIPCITY Y
ON Z.CITYID = Y.CITYID
LEFT OUTER JOIN CGCITY C
ON Y.CITYID = C.CITYID
LEFT OUTER JOIN CGSTATE S
ON C.STATEFIPS = S.STATEFIPS
LEFT OUTER JOIN UNIT U
ON L.UNITID = U.UNIT_ID
WHERE
L.LOCATION IN ('1', '2', '3', '4', '5')
UNION ALL
SELECT
L.LOCATION AS "LOC",
C.CITY_NAME AS "CIT",
H.STREET_NAME AS "STR",
U.APT_NAME AS "APT",
U.NUM AS "NUM",
L.DESCRIPTION AS "DES",
H.POP AS "POP",
S.STATE_CODE AS "STA",
H.SITE_NAME AS "SIT",
U.COUNTY AS "COU",
L.DESCRIPTION AS "PRO"
FROM
LOCATIONS L
LEFT OUTER JOIN CGZIP9 Z
ON L.L_ZIPCODE = Z.ZIPCODE
LEFT OUTER JOIN STREET_V H
ON L.LOCATION = H.LOCATION
LEFT OUTER JOIN CGZIPCITY Y
ON Z.CITYID = Y.CITYID
LEFT OUTER JOIN CGCITY C
ON Y.CITYID = C.CITYID
LEFT OUTER JOIN CGSTATE S
ON C.STATEFIPS = S.STATEFIPS
LEFT OUTER JOIN UNIT U
ON L.UNITID = U.UNIT_ID
WHERE
L.LOCATION IN ('1', '2', '4');
删除订单并尝试合并所有。