MySQL合并查询的JOIN问题——覆盖或合并同一个表中的两行

发布于 2024-10-16 02:34:48 字数 1276 浏览 9 评论 0原文

这很难解释,所以我会尽力解释。

我有一个由默认行(由 NULL specificSiteID 表示)和覆盖行(由 NON-NULL specificSiteID 表示)组成的表s.)

两种类型的行都使用genericID来确定正在加载的数据——有时,某些数据可能没有默认值。

编辑 1: 我引用的非空 specificSiteID 仅当其 siteID 与我正在查询的站点 ID 匹配时才会覆盖默认值。我不会将所有覆盖应用到一个默认值 - 我仅将 specificSiteID 的覆盖应用到默认值。 end edit 1

我对此查询有四个要求:

  1. 当既没有默认值也没有特定于行的覆盖时,不返回任何数据。
  2. 当没有默认数据时,返回覆盖
  3. 数据 当覆盖数据不为空时,返回覆盖默认数据的覆盖数据
  4. ,最后,当默认数据存在且不存在覆盖数据时,仅返回默认数据。

现在,我的 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 其中 specificSiteIDNULL,即使存在覆盖数据,也不会返回任何数据。

我正在寻找什么样的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 specificSiteIDs.)

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 specificSiteIDs 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:

  1. Return no data when there is neither a default nor a line-specific override.
  2. Return the override data when there is no default data
  3. Return the Override data overwriting the default data where the override data is not null
  4. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

樱花细雨 2024-10-23 02:34:48

由于Mysql不支持全连接,但是可以使用“左连接”+“右连接”来模拟。

我认为 genericID 34 和 SpecificSiteID 84 是示例,您需要连接所有 genericID,其中 SpecificSiteId 为 null(默认)且不为 null(覆盖),不是吗?

SELECT COALESCE( override.Col1 , default.Col1) as Col1,
       COALESCE(override.Col2, default.Col2) as Col2
  FROM (SELECT * from tableA where specificSiteID IS NULL ) as default
        LEFT JOIN  (SELECT * from tableA where specificSiteID is not null ) as override
               ON default.genericID = override.genericID
UNION 
SELECT COALESCE( override.Col1 , default.Col1) as Col1,
       COALESCE(override.Col2, default.Col2) as Col2
  FROM (SELECT * from tableA where specificSiteID IS NULL ) as default
        RIGHT JOIN  (SELECT * from tableA where specificSiteID is not null ) as override
               ON default.genericID = override.genericID

但是查看这个查询似乎您的数据库设计并不更适合您的情况

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?

SELECT COALESCE( override.Col1 , default.Col1) as Col1,
       COALESCE(override.Col2, default.Col2) as Col2
  FROM (SELECT * from tableA where specificSiteID IS NULL ) as default
        LEFT JOIN  (SELECT * from tableA where specificSiteID is not null ) as override
               ON default.genericID = override.genericID
UNION 
SELECT COALESCE( override.Col1 , default.Col1) as Col1,
       COALESCE(override.Col2, default.Col2) as Col2
  FROM (SELECT * from tableA where specificSiteID IS NULL ) as default
        RIGHT JOIN  (SELECT * from tableA where specificSiteID is not null ) as override
               ON default.genericID = override.genericID

But looking this query seems that your DB design is not the more appropriate for your case

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文