作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
SELECT Table1.Filter, Table1.Condition, Combined.Data FROM Table1
LEFT JOIN
(SELECT Key, Data FROM IF(Table1.Filter, Table2, Table3))) AS Combined
ON Table1.Condition = Combined.Key
我想创建一个 MySQL View ,显示 Table1
的所有列,以及 Table2
或 Table3
的列,具体取决于字段在 Table1.Filter
上。
一个简单的解决方案是 LEFT JOIN
Table2
和 Table3
,在列上使用 NULL
不适用。有没有办法避免创建 2 列?我不能 UNION
Table2
和 Table3
因为它们可能包含相同的 Key
。
最佳答案
下面应该做你想做的:
SELECT t1.Filter, t1.Condition,
COALESCE(t2.Data, t3.Data) as Data
FROM Table1 t1 LEFT JOIN
Table2 t2
ON t1.Filter AND t2.Key = t1.Condition LEFT JOIN
Table3 t3
ON (NOT t1.Filter) AND t3.key = t1.condition;
您不能让条件选择 FROM
中的表。但是,您可以在 ON
条件中设置条件。
关于基于 IF 条件的两个表之一的 MySQL JOIN,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45156459/
我是一名优秀的程序员,十分优秀!