简体   繁体   English

MySQL:如何用不匹配的行联接两个表

[英]MySQL: How to join two tables with some rows that does not match

I have two MySQL Tables: 我有两个MySQL表:

table1: 表格1:

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

table2: 表2:

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

I want to get this result: 我想得到这个结果:

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

Despite there are no entries for time = 0:20 - 0:40 in table 2 this rows should not be skiped in the result. 尽管表2中没有时间= 0:20-0:40的条目,但不应在结果中跳过这些行。

This is precisely what left join s are for: 这正是left join的作用:

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

您可以使用myTime字段左加入。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM