2013-03-26 250 views
2

我嘗試了sql語句的不同變體,但我仍然在選擇「沒有行」。我不知道我要去哪裏錯了!SQL查詢:沒有選擇行

這裏有一個問題:

我必須列出了其經理是「綠色」的員工舉辦的會議的標題。

與查詢相關聯的表是:

Employee_C表:

EID NAME SALARY   MID 
--- -------------------- ----- 
e01 Wilson 53000 
e02 Smith  48000  e01 
e03 Jones  38000  e01 
e04 Loftus  41000 
e05 Fox  54000  e04 
e06 Smith  45000  e04 
e07 Green  48000 
e08 Fox  49000  e04 
e09 Wolf  41000  e04 
e10 Wang  32000  e01 
e11 Phillips 33000  e07 
e12 Liu   27000  e07 

Conference_C表:

CONFID    TITLE      LOCATION   SDATE 
------  --------------------  -------------------- --------- 
c00001 Hydroinformatics     Singapore   15-JUN-12 
c00002 Ecological_modeling    Berlin    15-JUL-12 
c00003 Computational_M     London    25-MAY-12 
c00004 Ecoinformatics     Boston    22-AUG-12 
c00005 Uncertainty_analysis    Athens    10-OCT-12 
c00006 Large_databases     Toronto   13-APR-12 
c00007 Systems_analysis     Boston    23-MAR-12 
c00008 Systems_integration    Tokyo    24-FEB-12 
c00009 Aquatic_biology     Helsinki   12-MAY-12 
c00010 Information_systems    Paris    08-JAN-12 
c00011 Simulation_modeling    Rome    01-SEP-12 
c00012 DSS        Melbourne   18-DEC-12 

Deals_C表:

EID CONFID 
--- ------ 
e02 c00001 
e03 c00001 
e05 c00001 
e06 c00001 
e03 c00002 
e08 c00002 
e09 c00002 
e10 c00002 
e03 c00003 
e05 c00003 
e06 c00004 
e08 c00005 
e09 c00005 
e10 c00005 
e06 c00005 
e11 c00006 
e12 c00006 
e05 c00007 
e06 c00007 
e08 c00007 
e09 c00008 
e10 c00008 
e11 c00008 
e02 c00009 
e12 c00009 
e10 c00010 
e02 c00011 
e03 c00011 
e05 c00011 
e12 c00012 
e06 c00012 

的SQL語句,我有是:

select C.Title 
from Conference_C C 
where C.ConfID in (select D.ConfID 
        from Deals_C D 
        where D.eid in (select E.eid 
            from Employee_C E 
            where E.Name = 'Green')); 

我「沒有選擇行」

+0

您沒有並且處理eid = 07。 – Mat 2013-03-26 18:54:51

+0

Green的EID爲e07,您在Deals_C表中沒有任何含e07 EID的行。 – 2013-03-26 18:55:35

+0

檢查我的答案爲什麼它沒有返回記錄 – 2013-03-26 18:59:52

回答

4

與您查詢的問題是要檢查錯誤的員工數據。你WHERE子句檢查EIDEID當它真正應該檢查Deals_C.EIDEmployees.MID

select C.Title 
from Conference_C C 
inner join 
(
    select D.ConfID, e.eid 
    from Deals_C D 
    inner join Employee_C e 
    on d.EID= e.EID 
    where exists (select * 
       from Employee_C e2 
       where E2.Name = 'Green' 
        and e.mid = e2.eid) 
) d 
    on c.CONFID = d.CONFID 

SQL Fiddle with Demo

EXISTS查詢返回擁有的Green姓的行,但你需要檢查Employee_C.MID的子查詢EID

這也可以寫成:

select C.Title 
from Conference_C C 
where C.ConfID in (select D.ConfID 
        from Deals_C D 
        inner join Employee_C e 
        on d.EID= e.EID 
        where exists (select * 
           from Employee_C e2 
           where E2.Name = 'Green' 
            and e.mid = e2.eid)); 

SQL Fiddle with Demo

+0

哦,沒錯!非常感謝您的幫助! – Navy 2013-03-26 20:00:34

0

我只是有這個問題。我選擇「沒有選擇行」的原因僅僅是因爲我的sqlcase設置爲UPPER,而我以小寫查詢。 即WHERE empName LIKE'_a%'; 我將sqlcase設置爲混合並解決了我的問題。看起來引號之間的字符串區分大小寫。希望能幫助到你。