从SQL中的两个表重复相同的行?

时间:2011-10-12 02:26:46

标签: sql

我有两张桌子。

1)empDetails

Field(id,section,name,domain)
Records('E_01','IT','RAN','AUTOMATION')
Records('E_01','IT','SAMU','EMBEDED')
Records('E_02','MECH','RAJ','AUTO')

2)empAddress

Field(id,section,address)
Records('E_01','IT','BANGALORE')
Records('E_01','IT','BANGALORE')

我的查询是

SELECT  t1.name,t1.domain,t2.address
FROM empDetails as t1 ,
     empAddress as t2
WHERE t1.id = 'E_001'
  AND t2.id='E_001' 
  AND t1.section = 'IT'
  AND t2.section = 'IT'**

但我正在重复行,如下所示:

RAN AUTOMATION BANGALORE
RAN EMBEDED BANGALORE
SAMU AUTOMATION BANGALORE
SAMU EMBEDED BANGALORE

如何正确获取?

我需要

RAN AUTOMATION BANGALORE
SAMU EMBEDED BANGALOR

请有人帮帮我吗?

3 个答案:

答案 0 :(得分:0)

select t1.name, t1.domain, t2.address
from empDetails as t1, empAddress as t2
where t1.id = 'E_001' and t2.id='E_001' and t1.section = 'IT' and t2.section = 'IT'

问题是from ..., ...。使用这样的逗号执行cross join,但您可能需要inner join

select t1.name, t1.domain, t2.address
from empDetails as t1 INNER JOIN empAddress as t2
ON --join condition goes here
where t1.id = 'E_001' and t2.id='E_001' and t1.section = 'IT' and t2.section = 'IT'

答案 1 :(得分:0)

SELECT  t1.name,t1.domain,t2.address
  FROM empDetails as t1
 INNER JOIN empAddress as t2
    ON t1.id=t2.id
 WHERE t1.id = 'E_001'
   AND t2.id='E_001' 
   AND t1.section = 'IT'
   AND t2.section = 'IT'
 GROUP BY t1.name

答案 2 :(得分:0)

试试这个..

SELECT t1.name,t1.domain,t2.address
  FROM empDetails as t1
 INNER JOIN empAddress as t2 on t1.id=t2.id
 WHERE t1.section = 'IT'
   AND t2.section = 'IT';