简体   繁体   中英

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

I have two MySQL Tables:

table1:

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

table2:

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.

This is precisely what left join s are for:

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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