MySQL:如何连接两个表的某些行不匹配

标签 mysql sql select join

我有两个 MySQL 表:

表 1:

myTime|foo
----------
00:00 |8
----------
00:10 |6
----------
00:20 |1
----------
00:30 |5
----------
00:40 |3
----------
00:50 |4
----------

表 2:

myTime|bar
----------
00:00 |6
----------
00:10 |10
----------
00:50 |5
----------

我想得到这个结果:

myTime|foo|bar
--------------
00:00 |8  |6
-------------
00:10 |6  |10
-------------
00:20 |1  |
-------------
00:30 |5  |
-------------
00:40 |3  |
-------------
00:50 |4  |5
-------------

尽管表 2 中没有时间 = 0:20 - 0:40 的条目,但不应在结果中跳过此行。

最佳答案

这正是 left join 的用途:

SELECT    table1.mytime, foo, bar
FROM      table1
LEFT JOIN table2 ON table1.mytime = table2.mytime

关于MySQL:如何连接两个表的某些行不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48183357/

相关文章:

php - MySQL查询结果波动

sql - 根据其他记录更新记录

sql - Postgres : How to do Composite keys?

select - Swift 中的复杂大小写值

r - 如果 Sequence <= 3 则将所有值解码为零,并保留某些信息

mysql - 选择auto_increment字段值

PHP 结果与 MYSQL 多对多表和 GROUP By

php - 将 excel 导入多个表 - PHP 和 MYSQL (codeigniter)

mysql - 使用 mysql 查询返回所有子类别

PHP Mysql连接问题