我有一个 View ,我通过一系列左连接将外键值替换为其引用值。
其中一个表可以在我加入的地方具有多对一关系
Example.exampleid = SLA.exampleId.
我的问题是 SLA 表有一列 SLa.type
可以是A
或B
然后是 SLA.value
会有一个号码。这在我的 View 中创建了一个重复行,唯一的区别是 SLA.type
和SLA.value
。
我希望它返回 SLA.type
所在的这些列是 A
当表中找不到给定的 example.exampleId
的内容时,仍然不会破坏 View
例如我的 View 选择查询的简洁形式:
Select Example.exampleId, SLA.type, SLA.value
FROM Example
LEFT JOIN SLA ON Example.exampleId = SLA.exampleId
WHERE SLA.type <> "B" OR SLA.type IS NULL or SLA.value IS NULL
example.exampleId 在 SLA 表中只会有两行,一行用于类型 A,一行用于类型 B。
任何想法将不胜感激!
最佳答案
移动SLA.type <> "B"
将条件添加到连接子句中,因为它仅在这种情况下适用于右侧列。在 weher 子句中,条件适用于整个数据集:
Select Example.exampleId, SLA.type, SLA.value
FROM Example
LEFT JOIN SLA ON Example.exampleId = SLA.exampleId and SLA.type <> "B"
不过,我会考虑使用 SLA.type = "A"
,以防万一您将引入新的 SLA 类型。
关于mysql - 根据左连接列值返回 View 中的一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37587046/