允许在使用空行连接后添加其他数据

时间:2015-01-15 00:07:11

标签: sqlite

我有一个左连接查询返回正确的行数(等于左表),但因为右表是空的,所以没有相应的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已经存在于Week01中,它将无法被覆盖。有什么想法吗?如果可以做到,那么上面的查询应该可以正常工作。

1 个答案:

答案 0 :(得分:1)

您实际上可以使用IFNULL

选择它
 SELECT
 IFNULL(Week01.RecNum, "Project List".RecNum
 ..

回答您获取项目列表中存在但不在Week01中的所有recNums的问题 你可以使用left join

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

或者您可以使用not exists子句

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