很抱歉,我可能没有正确解释..
所以我正在更新我的问题..
下面是场景...我必须加入许多这样的类似的表,并且有类似的单行表,对我来说很好,他们有不同的key1 ...如果我特意添加Key1 = key1那么我将放在这里..不知道该怎么做..
create TABLE ##ZZSP00(
empl BIGINT,
key1 INT,
sal decimal
)
INSERT INTO ##ZZSP00 ( empl, key1 ,sal ) VALUES ( 1, 2,10)
INSERT INTO ##ZZSP00 ( empl, key1 ,sal ) VALUES ( 1,3, 20)
INSERT INTO ##ZZSP00 ( empl, key1 ,sal ) VALUES ( 1, 4,30)
create TABLE ##ZZSP01(
empl BIGINT,
key1 INT,
descr VARCHAR(10)
)
INSERT INTO ##ZZSP01 ( empl, key1 ,descr ) VALUES ( 1, 2,'apple')
INSERT INTO ##ZZSP01 ( empl, key1 ,descr ) VALUES ( 1,3, 'orange')
INSERT INTO ##ZZSP01 ( empl, key1 ,descr ) VALUES ( 1,4, 'kiwi')
结果错误
- 有点迷你笛卡儿正在这里发生
SELECT DISTINCT * FROM ##ZZSP00 a
JOIN ##ZZSP01 b
ON a.empl=b.empl
正确的结果
SELECT DISTINCT * FROM ##ZZSP00 a
JOIN ##ZZSP01 b
ON a.empl=b.empl AND a.key1=b.key1
答案 0 :(得分:0)
尝试使用 GROUP BY id 将具有相同ID的所有数据分组
答案 1 :(得分:0)
您需要LEFT JOIN
table1
和table2
[key]
INNER JOIN
table2
和table3
[key2]
SELECT t1.[Key],t1.key2,t2.key2,t3.key2,t1.name,t2.name,t3.name
FROM table1 t1
LEFT JOIN table2 t2 ON t1.[key] = t2.[key]
INNER JOIN table3 t3 ON t2.[key2] = t3.[key2]
sqlfiddle:http://sqlfiddle.com/#!18/1f234/1
答案 2 :(得分:0)
试试这个:
Optional.ofNullable(allLists.get("fruits"))
OR
select
t1.key,
t1.key2,
t1.name,
t2.name,
t3.name
from table1 t1
left join table2 t2 on t1.key = t2.key
left join table3 t3 on t1.key = t3.key
group by t1.key
答案 3 :(得分:0)
很抱歉可能我没有正确解释..现在是情况...我必须加入许多这样的类似的表,并且有类似的单行表,对我来说很好,他们有不同的key1 ...如果我特意添加Key1 = key1,那么我将放在这里..不知道该怎么办..
create TABLE ##ZZSP00(
empl BIGINT,
key1 INT,
sal decimal
)
INSERT INTO ##ZZSP00 ( empl, key1 ,sal ) VALUES ( 1, 2,10)
INSERT INTO ##ZZSP00 ( empl, key1 ,sal ) VALUES ( 1,3, 20)
INSERT INTO ##ZZSP00 ( empl, key1 ,sal ) VALUES ( 1, 4,30)
create TABLE ##ZZSP01(
empl BIGINT,
key1 INT,
descr VARCHAR(10)
)
INSERT INTO ##ZZSP01 ( empl, key1 ,descr ) VALUES ( 1, 2,'apple')
INSERT INTO ##ZZSP01 ( empl, key1 ,descr ) VALUES ( 1,3, 'orange')
INSERT INTO ##ZZSP01 ( empl, key1 ,descr ) VALUES ( 1,4, 'kiwi')
结果错误
- 有点迷你笛卡儿正在这里发生
SELECT DISTINCT * FROM ##ZZSP00 a
JOIN ##ZZSP01 b
ON a.empl=b.empl
正确的结果
SELECT DISTINCT * FROM ##ZZSP00 a
JOIN ##ZZSP01 b
ON a.empl=b.empl AND a.key1=b.key1