场景:
在下面的每一个中,“创建日期”、“other_created_date”和“日期”都是一天(即 2012-01-03)
表 1:
领域:
编号 |创建日期
表 2:
领域:
编号 |表_1_fk |其他创建日期
表 3:
领域:
日期
目标:
我想做以下事情:
SELECT * FROM table_1
JOIN table_2
ON table_1.id = table_2.table_1_fk
FULL OUTER JOIN table_3
ON table_3.date = (
CASE
WHEN table_1.created_date > table_2.other_created_date THEN table_1.created_date
ELSE table_2.other_created_date
END
)
基本上,我对 (Table_1 + Table_2) JOINed on Table_3 感兴趣,如果第一个语句为真,我们将在 Table_1 的日期加入,如果第二个语句为真,我们将在 Table_2 的日期加入
这是可能的还是有更好的方法?
最佳答案
SELECT * FROM table_1
JOIN table_2
ON table_1.id = table_2.table_1_fk
FULL OUTER JOIN table_3
ON table_3.date = GREATEST(table_1.created_date,table_2.other_created_date)
关于mysql - SQL conditional JOIN - JOIN point defined based on joined_table 条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33354867/