S_ID | STATION_NAME
------ | ------
1 | PACKAGER1
2 | PACKAGER2
3 | PACKAGER3
4 | PACKAGER4
5 | PACKAGER5
6 | PACKAGER6
7 | PACKAGER7
C_ID | STATION_ID | TO_STATION_ID
------ | -----------| -------------
1 | 1 | 7
2 | 2 | 7
3 | 3 | 7
4 | 4 | 7
5 | 5 | 7
6 | 6 | 7
7 | 7 | 1
7 | 7 | 2
7 | 7 | 3
7 | 7 | 4
7 | 7 | 5
7 | 7 | 6
SELECT CC.STATION_ID AS NUM,
CASE WHEN CC.STATION_ID = S.S_ID THEN S.STATION_NAME END
AS "FROM STATION"
FROM CONNECTION CC,
STATIONS S
替换工作正常,但创建了许多其他具有空值的单元格。在所有12 * 7 = 82。如果两个值不相等,我该如何跳过这种情况。
NUM | FROM_STATION
------ | ------
1 | PACKAGER1
2 | null
3 | null
4 | null
5 | null
6 | null
7 | null
我想得到这个结果:
NUM | FROM_STATION | TO_STATION
------ | ------------ | ----------
1 | PACKAGER1 | Packager7
2 | PACKAGER2 | Packager7
3 | PACKAGER3 | Packager7
4 | PACKAGER4 | Packager7
5 | PACKAGER5 | Packager7
6 | PACKAGER6 | Packager7
7 | PACKAGER7 | Packager1
8 | PACKAGER7 | Packager2
9 | PACKAGER7 | Packager3
10 | PACKAGER7 | Packager4
11 | PACKAGER7 | Packager5
12 | PACKAGER7 | Packager6
答案 0 :(得分:1)
您想要做什么,而您的查询似乎与彼此无关。首先,从不在FROM
子句中使用逗号。 始终使用正确的JOIN
语法。
其次,我不知道CASE
的用途。我怀疑你想要这样的查询:
SELECT CC.STATION_ID AS NUM,
S.STATION_NAME AS "FROM STATION"
FROM CONNECTION CC LEFT JOIN
STATIONS S
ON CC.STATION_ID = S.S_ID;
至少,这似乎可以做一些有用的事情。