2015-01-15 74 views
0

我有一個左連接查詢返回正確的行數(等於左表),但由於右表是空的,沒有相應的ROWID的。如何將rowid(RecNum應該是相同的值)添加到空表中,以便我可以將數據添加到結果數據集中?這裏的查詢:允許額外的數據添加後,與空行加入

 
select 
    Week01.RecNum as RecNum, 
    Week01.UserName as UserName, 
    Week01.Day1Reg as Day1Reg, 
    Week01.Day1OT as Day1OT, 
    Week01.Day2Reg as Day2Reg, 
    Week01.Day2OT as Day2OT, 
    Week01.Day3Reg as Day3Reg, 
    Week01.Day3OT as Day3OT, 
    Week01.Day4Reg as Day4Reg, 
    Week01.Day4OT as Day4OT, 
    Week01.Day5Reg as Day5Reg, 
    Week01.Day5OT as Day5OT, 
    Week01.Day6Reg as Day6Reg, 
    Week01.Day6OT as Day6OT, 
    Week01.Day7Reg as Day7Reg, 
    Week01.Day7OT as Day7OT 
from 
    "Project List" 
left join 
    Week01 
on 
    "Project List".RecNum = Week01.RecNum 
and 
    Week01.UserName = "JustMe" 

,或者,如果不能真正做到,如何查詢在Week01將預先創建行條目(如果它們不存在),以同樣的RECNUM列匹配並用「JustMe」填寫用戶名?但是,問題是如果RecNum已經存在於第一週,它不能被覆蓋。有關於此的任何想法?如果可以這樣做,那麼上面的查詢應該很好。

回答

1

您可以使用IFNULL

SELECT 
IFNULL(Week01.RecNum, "Project List".RecNum 
.. 

要回答你獲得存在於項目列表中,但不是在Week01 你可以做使用left join

select p.RecNum 
from "Project List" p 
left join Week01 w 
on p.RecNum = w.RecNum 
where w.RecNum is NULL 

,或者你所有recNums的問題實際上是選擇它可以使用not exists子句

select p.RecNum 
from "Project List" p 
where not exists 
( select 1 from Week01 w 
    where p.RecNum = w.RecNum 
) 
+0

謝謝雷達! – user2021539

相關問題