Mysql SQL-选择在特定日期不存在的用户
我有两张桌子,一张供用户使用,另一张供他们使用。我需要显示特定日期不在考勤表上的用户 这是我的桌子:Mysql SQL-选择在特定日期不存在的用户,mysql,sql,join,xampp,union,Mysql,Sql,Join,Xampp,Union,我有两张桌子,一张供用户使用,另一张供他们使用。我需要显示特定日期不在考勤表上的用户 这是我的桌子: USER_TABLE | ID | Name | ------------------- | 1 | John | | 2 | Peter | | 3 | Anne | | 4 | May | 我需要选择2019-02-18上未出现的所有用户 所以结果应该是这样的 | ID | Name | ----------
USER_TABLE
| ID | Name |
-------------------
| 1 | John |
| 2 | Peter |
| 3 | Anne |
| 4 | May |
我需要选择2019-02-18上未出现的所有用户
所以结果应该是这样的
| ID | Name |
-------------------
| 1 | John |
| 3 | Anne |
谢谢。您可以尝试使用left join
SELECT a.id,name
FROM USER_TABLE a
LEFT JOIN ATTENDANCE_TABLE b ON a.id=b.id
AND b.date='2019-02-18'
AND b.id IS NULL
您可以尝试使用左连接
SELECT a.id,name
FROM USER_TABLE a
LEFT JOIN ATTENDANCE_TABLE b ON a.id=b.id
AND b.date='2019-02-18'
AND b.id IS NULL
试试这个查询
select ID, Name
from USER_TABLE UT
where ID not in (select ID from ATTENDANCE_TABLE where date = '2019-02-18')
试试这个查询
select ID, Name
from USER_TABLE UT
where ID not in (select ID from ATTENDANCE_TABLE where date = '2019-02-18')
这是不正确的,因为a未引用的日期不是引号,b它将返回ID 2两次。这是不正确的,因为a未引用的日期不是引号,b它将返回ID 2两次。您是指用户\表。。也许是离开了桌子?@danblack,是的,实际上我是这个意思-但写错了。感谢您识别我的错误。您是指用户表吗。。也许是离开了桌子?@danblack,是的,实际上我是这个意思-但写错了。感谢你发现我的错误谢谢因为我有办法解决这些错误谢谢因为我有办法解决这些错误
Drop table if exists my_users;
Create table my_users
(User_ID serial primary key
,Name varchar(100) not null unique
);
Insert into my_users values
(1,'John'),
(2,'Peter'),
(3,'Anne'),
(4,'May');
Drop table if exists my_ATTENDANCE;
Create table my_attendance
(user_ID INT NOT NULL
,Date date not null
,primary key(user_id,date)
);
Insert into my_attendance values
( 2 ,'2019-02-16'),
( 2 ,'2019-02-17'),
(2 ,'2019-02-18'),
(3 ,'2019-02-17'),
(4 ,'2019-02-18');
Select u.*
From my_users u
Left
Join my_attendance a
On a.user_id = u.user_id
and a.date = '2019-02-18'
Where a.user_id is null;
User_ID Name
3 Anne
1 John
Select UserTable.Id, name
From UserTable left join AttendanceTable
On UserTable.Id=AttendanceTable.Id
Where date! =#2019-02-18#