【发布时间】:2015-10-27 04:48:13
【问题描述】:
场景:
在下面的每一个中,“created date”、“other_created_date”和“date”都是一天(即 2012-01-03)
表 1:
领域:
身份证 |创建日期
表 2:
领域:
身份证 | table_1_fk | other_created_date
表 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 的日期加入
这是可能的还是有更好的方法?
【问题讨论】:
-
我宁愿以固定方式加入table3两次,并使用条件表达式在选择列表中选择正确的值。
-
@Shadow 你能提供一个查询示例吗?现在我把它分解成两个单独的查询,然后我只是用不同的语言添加这两组,但我正试图把这一切都放到 SQL 中
-
你能清理一下这个问题的标签吗
标签: mysql sql postgresql postgresql-9.3