简体   繁体   中英

MySQL LEFT JOIN with OR condition

I have a tables :

table_a.date
2018-05-13 17:30:22
2018-05-13 17:30:22
2018-05-13 17:30:22

and :

table_b.date
2018-05-13 17:30:22
2018-05-13 17:30:23
2018-05-13 17:30:23

I need to LEFT JOIN these tables with condition : table_a.date = table_b.date , or table_a.date = table_b.date + 1 second .

more or less like this :

SELECT * 
FROM table_a
LEFT JOIN table_b
ON table_a.date = table_b.date OR table_a.date = table_b.date (+ 1 second)

but I have no idea how to use OR condition as requirement of LEFT JOIN . what is the right SQL for this?

Use the DATE_ADD as below:

SELECT * 
FROM table_a
LEFT JOIN table_b
ON table_a.date = table_b.date
OR table_a.date = date_add(table_b.date, interval 1 second)

为什么您需要检查秒,只需将两个日期都设置为相同的格式并检查(如果diff仅用于秒),请删除秒,请使用DATE_FORMAT(date, format_mask)

SELECT * FROM table_a LEFT JOIN table_b ON DATE_FORMAT(table_a.date, "%M %d %Y %h %i") = (table_a.date, "%M %d %Y %h %i")

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