SQL JOIN ISSUE - 在这种情况下需要使用连接

时间:2014-12-08 09:44:12

标签: sql

我有桌子:

表1:

CODE NAME
---------
 A    1
 B    2
 C    3
 A    1
 A    1

表2:

CODE NAME CODE1 NAME1   
-----------------------
 A    1    B     2
 B    2    C     3
 C    3    A     1
 A    1    A     1
 A    1    A     1

我想将table1.codetable2.codetable2.code1匹配。根据代码匹配,我想在表2 fileds(NAME,NAME1)中显示Table1中的Name。请帮忙,我该怎么做?

2 个答案:

答案 0 :(得分:0)

尝试类似:

SELECT t1.NAME, t2.NAME1
FROM   table1 t1 INNER JOIN table2 t2 
ON     t1.code = t2.code
AND    t1.Name = t2.Name  --if you need matching on table1's code change t2.Name to t2.Name1
WHERE  t1.code = t2.code1 --if you need matching on table1's code change t2.code to t1.code and 

将WHERE替换为AND

答案 1 :(得分:0)

尝试按两个表格查看您的table2,并使用不同的代码分别JOIN查看它们。例如:

select t1.code
, t1.name [t1_name]
, t2.name [t2_name]
, t3.name1 [t2_name1]
from table1 t1
left join table2 t2 on t1.code = t2.code
left join table2 t3 on t1.code = t3.code1