[英]MySQL select rows from one table not in another for specific dates
I've two table individuals and Staff login times as mention bellow. 我有两个表个人和职员登录时间,如下所述。 I want to fetch staff members those who are not logged In on specific date.
我想获取在特定日期未登录的人员。
individuals
+----+------+--------------+
| id | name | phone_number |
+----+------+--------------+
| 1 | Smith | 1234567897 |
+----+------+--------------+
| 2 | David | 9874561237 |
+----+------+--------------+
| 3 | John | 7778889991 |
+----+------+--------------+
staff_login_times
+----+------+--------------+
| id | individual_id | login_date |
+----+------+--------------+
| 1 | 2 | 2017-02-27 |
+----+------+--------------+
| 2 | 1 | 2017-02-26 |
+----+------+--------------+
| 3 | 2 | 2017-02-26 |
+----+------+--------------+
| 4 | 3 | 2017-02-26 |
+----+------+--------------+
How do I find out individuals not present on 27th 我如何找出27日不在场的人
Result
+----+------+--------------+
| id | name | phone_number |
+----+------+--------------+
| 1 | Smith | 1234567897 |
+----+------+--------------+
| 3 | John | 7778889991 |
+----+------+--------------+
Query I have tried: 我尝试过的查询:
SELECT *
FROM individuals
LEFT JOIN staff_login_times ON individuals.id = staff_login_times.individual_id
WHERE staff_login_times.individual_id IS NULL
AND staff_login_times.login_date = '2017-02-27'
Just small change, use your and condition with join statement:- 只是很小的变化,使用您的条件和join语句即可:-
SELECT *
FROM individuals
LEFT JOIN staff_login_times ON individuals.id = staff_login_times.individual_id AND staff_login_times.login_date = '2017-02-27'
WHERE staff_login_times.individual_id IS NULL
you don't need join 你不需要加入
try this 尝试这个
select * from individuals
where id not in
(select individual_id from staff_login_times where login_date = '2017-02-27');
I hope this helps: 我希望这有帮助:
SELECT a.id, a.`name`, a.phone_number, b.login_date FROM individuals a LEFT JOIN
staff_login_times b on a.id = b.individual_id WHERE b.login_date <> "2017-02-27";
Another option you can use , is to expand where
condition instead of LEFT JOIN
您可以使用的另一个选项是
where
条件而不是LEFT JOIN
扩展
SELECT individuals.id ,individuals.name FROM individuals ,staff_login_times WHERE
staff_login_times.login_date = '2017-02-27'
AND staff_login_times.individual_id != individuals.id ;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.