如何消除连接sql语句中的重复结果

发布于 2024-12-18 01:43:56 字数 611 浏览 0 评论 0原文

我有四个这样相关的表:

TABLE A: 1 to many with TABLE B
TABLE B: 1 to many with TABLE C
TABLE D: many to 1 with TABLE B

我想创建一个不包含重复项的结果集。

SELECT A.f1
       A.f2
       B.f1
       C.f1
       D.f1
from   A LEFT JOIN (B INNER JOIN D on D.fk_b = B.id) on A.id = B.fk_a
         LEFT JOIN C on C.fk_b = B.id

一些但不是所有记录都是重复的。仅TABLE D 中与TABLE B 具有相同FK 的记录。

如果有 2 个相同的 D.fk_B 字段,那么我只想选择第一个。我的 INNER JOIN 会变成这样:

B INNER JOIN TOP 1 D on..... 

但这不可能?

谢谢你!

i have four tables which are related like this:

TABLE A: 1 to many with TABLE B
TABLE B: 1 to many with TABLE C
TABLE D: many to 1 with TABLE B

I would like to create a result set which contains no duplicates.

SELECT A.f1
       A.f2
       B.f1
       C.f1
       D.f1
from   A LEFT JOIN (B INNER JOIN D on D.fk_b = B.id) on A.id = B.fk_a
         LEFT JOIN C on C.fk_b = B.id

Some but not all records are duplicated. Only the records from TABLE D which have the same FK to TABLE B.

If there are 2 the same D.fk_B fields then i would like to select only the first one. My INNER JOIN would become something like:

B INNER JOIN TOP 1 D on..... 

But that's not possible?

Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

夏日浅笑〃 2024-12-25 01:43:56

假设所讨论的结构接近下图所示的结构,

<块引用>

表格

以下 SELECT 语句将仅显示主键,简要介绍返回的数据。

SELECT
  A.id AS `A_id`,
  B.id AS `B_id`,
  COALESCE(C.id, 0) AS `C_id`,
  COALESCE(
    (SELECT D.id FROM D WHERE D.fk_b = B.id LIMIT 1), 0
  ) AS `D_id`
FROM B
INNER JOIN A ON (B.fk_a = A.id)
LEFT JOIN C ON (B.id = C.fk_b);

现在您要求的选择:

SELECT
  A.f1 AS `A_f1`,
  A.f2 AS `A_f2`,
  B.f1 AS `B_f1`,
  COALESCE(C.f1, '-') AS `C_f1`,
  COALESCE(
    (SELECT D.f1 FROM D WHERE D.fk_b = B.id LIMIT 1), '-'
  ) AS `D_f1`
FROM B
INNER JOIN A ON (B.fk_a = A.id)
LEFT JOIN C ON (B.id = C.fk_b);

上面的代码已经过测试并且在 MySQL 5.6 上运行良好

Assuming that the structure in question is near the one shown bellow,

Tables

The following SELECT statement will show you just the primary keys, briefly giving an idea about what data is being returned.

SELECT
  A.id AS `A_id`,
  B.id AS `B_id`,
  COALESCE(C.id, 0) AS `C_id`,
  COALESCE(
    (SELECT D.id FROM D WHERE D.fk_b = B.id LIMIT 1), 0
  ) AS `D_id`
FROM B
INNER JOIN A ON (B.fk_a = A.id)
LEFT JOIN C ON (B.id = C.fk_b);

Now the select you've asked for:

SELECT
  A.f1 AS `A_f1`,
  A.f2 AS `A_f2`,
  B.f1 AS `B_f1`,
  COALESCE(C.f1, '-') AS `C_f1`,
  COALESCE(
    (SELECT D.f1 FROM D WHERE D.fk_b = B.id LIMIT 1), '-'
  ) AS `D_f1`
FROM B
INNER JOIN A ON (B.fk_a = A.id)
LEFT JOIN C ON (B.id = C.fk_b);

Code above was tested and worked fine on MySQL 5.6

韶华倾负 2024-12-25 01:43:56

看来您有笛卡尔连接。

您可以尝试将 DISTINCT 放在列定义前面,或者可以使用子查询将表链接在一起。

It appears that you have cartesian join.

You could try just putting DISTINCT infront of you column definition or you could so subqueries to link the tables together.

悲欢浪云 2024-12-25 01:43:56

尝试

SELECT DISTINCT A.f1
       A.f2
       B.f1
       C.f1
       D.f1
from   A LEFT JOIN (B INNER JOIN D on D.fk_b = B.id) on A.id = B.fk_a
         LEFT JOIN C on C.fk_b = B.id

你可以这样做:

B INNER JOIN TOP 1 D on..... 

像这样

B INNER JOIN (SELECT TOP 1 fields from table) D ON ...

尝试这个:

  SELECT DISTINCT A.f1
           A.f2
           B.f1
           C.f1
           D.f1
    from   A 
    LEFT JOIN (B INNER JOIN (SELECT DISTINCT fk_b FROM D) D on D.fk_b = B.id) 
         on A.id = B.fk_a
    LEFT JOIN C on C.fk_b = B.id

Try

SELECT DISTINCT A.f1
       A.f2
       B.f1
       C.f1
       D.f1
from   A LEFT JOIN (B INNER JOIN D on D.fk_b = B.id) on A.id = B.fk_a
         LEFT JOIN C on C.fk_b = B.id

You can do this:

B INNER JOIN TOP 1 D on..... 

Like this

B INNER JOIN (SELECT TOP 1 fields from table) D ON ...

Try this:

  SELECT DISTINCT A.f1
           A.f2
           B.f1
           C.f1
           D.f1
    from   A 
    LEFT JOIN (B INNER JOIN (SELECT DISTINCT fk_b FROM D) D on D.fk_b = B.id) 
         on A.id = B.fk_a
    LEFT JOIN C on C.fk_b = B.id
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文