我有两个表,一个用于用户,另一个用于他们的出勤。我需要显示某个日期不在考勤表上的用户。
这是我的桌子:
USER_TABLE
| ID | Name |
-------------------
| 1 | John |
| 2 | Peter |
| 3 | Anne |
| 4 | May |
ATTENDANCE_TABLE
| ID | Date |
--------------------------------
| 2 | 2019-02-16 |
| 2 | 2019-02-17 |
| 2 | 2019-02-18 |
| 3 | 2019-02-17 |
| 4 | 2019-02-18 |
我需要选择所有不在“ 2019-02-18”上的用户。
所以结果应该像这样。
| ID | Name |
-------------------
| 1 | John |
| 3 | Anne |
谢谢。
答案 0 :(得分:3)
尝试此查询,
select ID, Name
from USER_TABLE UT
where ID not in (select ID from ATTENDANCE_TABLE where date = '2019-02-18')
答案 1 :(得分:3)
select ID, Name
from USER_TABLE UT
where ID not in (select ID from ATTENDANCE_TABLE where date = '2019-02-18')
答案 2 :(得分:2)
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
答案 3 :(得分:0)
您可以尝试使用左联接
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
答案 4 :(得分:-1)
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/css/bootstrap.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/js/bootstrap.min.js"></script>
<div class="form-group col-xs-6 col-sm-6 col-md-6 col-lg-3">
<label for="gstCodeInput">GST Code</label>
<div class="input-group">
<input type="text" class="form-control" aria-label="Text input with dropdown button" id="gstCodeInput">
<div class="input-group-append">
<button class="btn btn-outline-secondary dropdown-toggle" type="button" data-toggle="dropdown" aria-haspopup="false" aria-expanded="false"></button>
<div class="dropdown-menu dropdown-menu-right" id="gstCodeDropdown" data-val=''>
<a class="dropdown-item" href="#">gst Code 1</a>
<a class="dropdown-item" href="#">gst Code 2</a>
<a class="dropdown-item" href="#">gst Code 3</a>
</div>
</div>
</div>
</div>