我有两张桌子
员工表
id username status
1 user1 retire
2 user2 retire
3 user3 working
4 user4 working
5 user5 working
6 user6 working
7 user7 retire
报告表
id username task date
1 user3 task1 2018-02-15
2 user3 task2 2018-02-15
3 user3 task3 2018-02-15
4 user4 task1 2018-02-15
5 user4 task2 2018-02-15
6 user4 task3 2018-02-15
7 user4 task1 2018-02-16
8 user5 task1 2018-02-16
9 user5 task2 2018-02-16
10 user5 task3 2018-02-16
11 user5 task4 2018-02-16
12 user3 task2 2018-02-16
13 user3 task3 2018-02-16
我要做的是,首先从员工表中选择有状态工作的用户名,并将其与特定日期的报表中的用户名进行比较。我使用左外连接相同,它给了我结果但不完全是我的意图,我对外连接的查询是
SELECT DISTINCT employee.username FROM employee LEFT OUTER JOIN report
ON employee.username = report.username WHERE report.username IS null And
employee.status = 'working'
我想从报告表中添加更多日期条件,但我无法得到它,有人可以建议我如何获得所需的结果。
答案 0 :(得分:1)
SELECT Distinct(employee.username) FROM employee where employee.status = 'working'
and employee.username not in (SELECT report.username from report where
report.date = 'YourDate')
答案 1 :(得分:0)
也许这样的事情。
Select * from ReportTable
where taskDate between @minValue and @maxValue
and userName in (select username from Employee where status = 'working')
答案 2 :(得分:0)
你的问题似乎不完整,但据我所知,我还是试着回答。看起来你试图在where子句中添加日期过滤器,但是你已经使用了EMPLOYEE TABLE的REPORT TABLE“LEFT OUTER JOIN”。最好不要使用where子句条件来过滤LEFT OUTER JOINed Table中的记录,而是可以在ON子句中使用该条件(在哪里使用LEFT OUTER JOIN)
此致
Ananda Kumar J。
答案 3 :(得分:0)
LEFT OUTER JOIN
错误的SELECT DISTINCT employee.username FROM report
LEFT OUTER JOIN employee ON employee.username = report.username
WHERE report.username IS null And employee.status = 'working'
and taskDate between @minValue and @maxValue