空行返回的MySQL SELECT JOIN [英] MySQL SELECT JOIN with empty row returns
本文介绍了空行返回的MySQL SELECT JOIN的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下MySQL表:
I have the following MySQL tables:
[用户]
| id | name |
|----|---------|
| 1 | John |
| 2 | Anna |
| 3 | Peter |
[次]
| user_ID | date | time |
|---------|------------|--------|
| 1 | 2020-03-20 | 07:00 |
| 1 | 2020-03-21 | 08:00 |
| 3 | 2020-03-22 | 09:00 |
我的查询如下:
SELECT name, date, time
FROM users
INNER JOIN times ON times.user_ID = users.id
WHERE date = '2020-03-22';
我得到的是:
| name | date | time |
|---------|------------|--------|
| Peter | 2020-03-22 | 09:00 |
我想要的是:
| name | date | time |
|---------|------------|--------|
| John | | |
| Anna | | |
| Peter | 2020-03-22 | 09:00 |
有没有一种方法可以将times表中的不存在的行(不是字段!)与users表连接起来?
is there a way to join non existent lines (not fields!) in the times table with the users table?
推荐答案
使用left join
并将where
子句移到on
:
SELECT name, date, time
FROM users
left JOIN times ON times.user_ID = users.id and date = '2020-03-22';
这篇关于空行返回的MySQL SELECT JOIN的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文