請通過以下問題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
您是否嘗試過'CROSS JOIN'而不是'UNION'? – luckystars 2013-02-09 09:06:16
關於你想要的結果的開始和結束日期? – 2013-02-09 09:06:28
嗨Raheen,幾乎有但Id和Leader不相關,因此不能使用ON pl.leader = p.id. Id是一個計數器,可以是任何值,Leader是一個指標,只能是0或1。謝謝 – user1254513 2013-02-09 09:39:47