2013-02-09 61 views
1

請通過以下問題mSQL的連接的查詢

幫助排結果我有一個表:人

Id Leader Name1 Name2 StartDate EndDate 
123 1  Person1   2013-02-11 2013-02-17 
123 0    Person2 2013-02-13 2013-02-13 
123 0    Person3 2013-02-13 2013-02-13 

我有一個QUERY1

(SELECT t1.Id, t1.Name1, t1.Name2 
FROM `dbo`.`people` t1 
WHERE t1.StartDate >= '2013-02-11' 
AND t1.Leader = 1) 

UNION 

(SELECT t2.Id, t2.Name1, t1.Name2 
FROM `dbo`.`people` t2 
WHERE t2.StartDate >= '2013-02-11' 
AND t2.Leader = 0) 

這將返回

Id Name1 Name2 
123, Person1 

我有一個查詢2

(SELECT t1.Id, t1.Name1, t1.Name2 
FROM `dbo`.`people` t1 
WHERE t1.StartDate >= '2013-02-13' 
AND t1.Leader = 1) 

UNION 

(SELECT t2.Id, t2.Name1, t1.Name2 
FROM `dbo`.`people` t2 
WHERE t2.StartDate >= '2013-02-13' 
AND t2.Leader = 0) 

這將返回

Id Name1 Name2 
123, Person1 
123,   Person2 
123,   Person3 

我需要爲2012-02-11的結果顯示

Id Name1 Name2 
123, Person1 null 

併爲2012-02-13顯示

Id Name1 Name2 
123, Person1 Person2 
123, Person1 Person3 
+0

您是否嘗試過'CROSS JOIN'而不是'UNION'? – luckystars 2013-02-09 09:06:16

+0

關於你想要的結果的開始和結束日期? – 2013-02-09 09:06:28

+0

嗨Raheen,幾乎有但Id和Leader不相關,因此不能使用ON pl.leader = p.id. Id是一個計數器,可以是任何值,Leader是一個指標,只能是0或1。謝謝 – user1254513 2013-02-09 09:39:47

回答

1

這是一個滿足您所需結果的查詢

(SELECT 
    id, 
    name1, 
    IFNULL(name2, 'No one available') AS name2, 
    startdate AS `date` 
FROM 
    person 
WHERE startdate = '2013-02-11') 
UNION 
ALL 
(SELECT 
    p.id, 
    IFNULL(p.name1, pl.name1) AS name1, 
    IFNULL(p.name2, 'Noone available') AS name2, 
    p.startdate AS `DATE` 
FROM 
    person AS p 
    LEFT JOIN person AS pl 
    ON pl.leader = p.id 
WHERE p.startdate = '2013-02-13') 

輸出

id name1  name2    date 
--------------------------------------------------- 
1 Person1  No one available 2013-02-11 
1 Person1  Person2    2013-02-13 
1 Person1  Person3    2013-02-13 

點擊Here用於演示

,這裏是你的查詢與修改一點

(SELECT 
    t1.Id, 
    t1.Name1, 
    IFNULL(t1.Name2, 'Null') AS Name2 
FROM 
    `dbo`.`people` t1 
WHERE t1.StartDate >= '2013-02-13' 
    AND t1.Leader = 1) 
UNION 
(SELECT 
    t2.Id, 
    IFNULL(t2.Name1, p.leader) AS Name1, 
    t1.Name2 
FROM 
    `dbo`.`people` t2 
    LEFT JOIN people AS p 
    ON p.leader = t2.id 
WHERE t2.StartDate >= '2013-02-13' 
    AND t2.Leader = 0) 

EDITS

另一種方法爲t Ø使用子查詢

(SELECT 
    t1.Id, 
    t1.Name1, 
    IFNULL(t1.Name2, 'Null') AS Name2 
FROM 
    `dbo`.`people` t1 
WHERE t1.StartDate >= '2013-02-13' 
    AND t1.Leader = 1) 
UNION 
(SELECT 
    t2.Id, 
    IFNULL(t2.Name1, (SELECT name1 FROM people WHERE t2.StartDate = '2013-02-13')) AS Name1, 
    t2.Name2 
FROM 
    `dbo`.`people` t2 
WHERE t2.StartDate >= '2013-02-13' 
    AND t2.Leader = 0) 

刪除連接和而選擇IFNULL進來名1在聯盟的第二部分使用子查詢。 如果Name1字段爲空或非空,此查詢將從表IFNull檢查中提取結果。如果這是空的,則執行作爲子查詢的IFNULL的第二個參數並獲取名稱。如果您需要更多幫助,請對Google進行一些調查。

+0

嗨Raheen,幾乎有但Id和Leader不相關,因此不能使用ON pl.leader = p.id. Id是一個計數器,可以是任何值,Leader是一個指標,只能是0或1。謝謝 – user1254513 2013-02-09 09:54:32

+0

嗨拉欣,我是新手對不起,你能舉個例子來幫助我。 – user1254513 2013-02-09 10:30:40

+0

@ user1254513請查看更新,並嘗試對Google或Stackoverflow進行一些調查。對不起我很忙。 – 2013-02-09 10:36:03