MySQL合并查询的JOIN问题——覆盖或合并同一个表中的两行
这很难解释,所以我会尽力解释。
我有一个由默认行(由 NULL
specificSiteID
表示)和覆盖行(由 NON-NULL
specificSiteID 表示)组成的表
s.)
两种类型的行都使用genericID
来确定正在加载的数据——有时,某些数据可能没有默认值。
编辑 1: 我引用的非空 specificSiteID
仅当其 siteID 与我正在查询的站点 ID 匹配时才会覆盖默认值。我不会将所有覆盖应用到一个默认值 - 我仅将 specificSiteID
的覆盖应用到默认值。 end edit 1
我对此查询有四个要求:
- 当既没有默认值也没有特定于行的覆盖时,不返回任何数据。
- 当没有默认数据时,返回覆盖
- 数据 当覆盖数据不为空时,返回覆盖默认数据的覆盖数据
- ,最后,当默认数据存在且不存在覆盖数据时,仅返回默认数据。
现在,我的 SQL 查询看起来像 --
SELECT COALESCE( override.Col1 , default.Col1) as Col1,
COALESCE(override.Col2, default.Col2) as Col2
FROM
(SELECT * from tableA where
genericID = 84 AND
specificSiteID IS NULL ) as default
LEFT JOIN
(SELECT * from tableA where
genericID = 84 AND
specificSiteID = 34 ) as override
ON default.genericID = override.genericID;
该脚本的问题是第 2 种情况由于 LEFT JOIN
而失败。 当没有LEFT JOIN
的默认行时,不包含覆盖行。使用上面的示例,如果没有 genericID
= 84 其中 specificSiteID
为 NULL
,即使存在覆盖数据,也不会返回任何数据。
我正在寻找什么样的JOIN
?我什至可以用 JOIN
来覆盖它吗?
It's difficult to explain this, so I'll do my best.
I have a table that consists of default rows (noted by a NULL
specificSiteID
) and override rows (noted by NON-NULL
specificSiteID
s.)
Both types of rows use genericID
to determine the data being loaded-- sometimes, there may be no defaults for some data.
edit 1: The Non-null specificSiteID
s I refer to only override a default when their siteID matches the one I am querying for. I don't apply all overrides to one default-- I only apply a specificSiteID
's override to the default.
end edit 1
I have four requirements of this query:
- Return no data when there is neither a default nor a line-specific override.
- Return the override data when there is no default data
- Return the Override data overwriting the default data where the override data is not null
- and finally, Return only the default data when it exists and there is no override data.
Right now, my SQL Query looks like--
SELECT COALESCE( override.Col1 , default.Col1) as Col1,
COALESCE(override.Col2, default.Col2) as Col2
FROM
(SELECT * from tableA where
genericID = 84 AND
specificSiteID IS NULL ) as default
LEFT JOIN
(SELECT * from tableA where
genericID = 84 AND
specificSiteID = 34 ) as override
ON default.genericID = override.genericID;
The problem with this script is that case #2 fails due to the LEFT JOIN
. When there's no default row to LEFT JOIN
upon, the override row isn't included. Using the above example, if there is no genericID
= 84 where specificSiteID
is NULL
, no data is returned, even if there is override data.
What kind of JOIN
am I looking for? Can I even cover this with a JOIN
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于Mysql不支持全连接,但是可以使用“左连接”+“右连接”来模拟。
我认为 genericID 34 和 SpecificSiteID 84 是示例,您需要连接所有 genericID,其中 SpecificSiteId 为 null(默认)且不为 null(覆盖),不是吗?
但是查看这个查询似乎您的数据库设计并不更适合您的情况
As Mysql doesn't support the full join, but you can simulate it using "left join" + "right join".
I think that the genericID 34 and the specificSiteID 84 are examples, and you need the join of all genericIDs where the specificSiteId is null (default) and is not null (override), don't you?
But looking this query seems that your DB design is not the more appropriate for your case