如何正确地将通过 id 链接的表中的行添加到 MySQL 上的另一个表?

发布于 2025-01-12 02:11:48 字数 2610 浏览 1 评论 0原文

假设以下查询:

SELECT author, is_public, content
FROM dapp.messages WHERE is_public = '0'

获取下表:

| author| is_public      | content            |
-----------------------------------------------
|  3240 |0               |Hello, I'm Bertha   |
|  4039 |0               |Hello, I'm Kristina |
|  4810 |0               |Hello, I'm April    |

现在,在本例中,author 列中的数字链接到另一个名为 credentials 的表中的行,这意味着即:

3240 链接到 credentials 表中的这一行:

|   id  | first_name   | member_type   |
----------------------------------------
|  3240 |Bertha        | regular       |

4039 链接到 credentials 表中的这一行:

|   id  | first_name   | member_type   |
----------------------------------------
|  4039 |Kristina      | regular       |

4810 链接到 credentials 表中的这一行:

|   id  | first_name   | member_type   |
----------------------------------------
|  4039 |April         | regular       |

因此,我想知道从 添加 first name 列的正确方法将 >credentials 表添加到本文中第一个查询获得的表,最终得到如下输出:

| author| is_public      | content            | first_name   |
--------------------------------------------------------------
|  3240 |0               |Hello, I'm Bertha   |Bertha        |
|  4039 |0               |Hello, I'm Kristina |Kristina      |
|  4810 |0               |Hello, I'm April    |April         |

我认为通过尝试以下查询,我将从上面获得所需的输出:

SELECT t1.*, t2.*
FROM (SELECT author, is_public, content
    FROM dapp.messages
    WHERE is_public = '0') t1
CROSS JOIN (SELECT first_name
    FROM dapp.credentials
    WHERE id IN (SELECT author FROM dapp.messages)) t2 

但我最终创建了一个抛出这个输出的怪物

| author| is_public      | content            | first_name   |
--------------------------------------------------------------
|  3240 |0               |Hello, I'm Bertha   |Bertha        |
|  3240 |0               |Hello, I'm Bertha   |Kristina      |
|  3240 |0               |Hello, I'm Bertha   |April         |
|  4039 |0               |Hello, I'm Kristina |Bertha        |
|  4039 |0               |Hello, I'm Kristina |Kristina      |
|  4039 |0               |Hello, I'm Kristina |April         |
|  4810 |0               |Hello, I'm April    |Bertha        |
|  4810 |0               |Hello, I'm April    |Kristina      |
|  4810 |0               |Hello, I'm April    |April         |

:我根本没有得到,所以我被困住了。

Suppose that the following query:

SELECT author, is_public, content
FROM dapp.messages WHERE is_public = '0'

gets the following table:

| author| is_public      | content            |
-----------------------------------------------
|  3240 |0               |Hello, I'm Bertha   |
|  4039 |0               |Hello, I'm Kristina |
|  4810 |0               |Hello, I'm April    |

Now, in this case, the numbers in the column author are linked to a row in another table called credentials, which means that:

3240 is linked to this row in credentials table:

|   id  | first_name   | member_type   |
----------------------------------------
|  3240 |Bertha        | regular       |

4039 is linked to this row in credentials table:

|   id  | first_name   | member_type   |
----------------------------------------
|  4039 |Kristina      | regular       |

4810 is linked to this row in credentials table:

|   id  | first_name   | member_type   |
----------------------------------------
|  4039 |April         | regular       |

So, I would like to know a correct way of adding the first name column from credentials table to the table obtained by the first query in this post, ending up with an output like this:

| author| is_public      | content            | first_name   |
--------------------------------------------------------------
|  3240 |0               |Hello, I'm Bertha   |Bertha        |
|  4039 |0               |Hello, I'm Kristina |Kristina      |
|  4810 |0               |Hello, I'm April    |April         |

I thought that by trying the following query, I would get the desired output from above:

SELECT t1.*, t2.*
FROM (SELECT author, is_public, content
    FROM dapp.messages
    WHERE is_public = '0') t1
CROSS JOIN (SELECT first_name
    FROM dapp.credentials
    WHERE id IN (SELECT author FROM dapp.messages)) t2 

But I ended up creating a monstrosity that threw this output:

| author| is_public      | content            | first_name   |
--------------------------------------------------------------
|  3240 |0               |Hello, I'm Bertha   |Bertha        |
|  3240 |0               |Hello, I'm Bertha   |Kristina      |
|  3240 |0               |Hello, I'm Bertha   |April         |
|  4039 |0               |Hello, I'm Kristina |Bertha        |
|  4039 |0               |Hello, I'm Kristina |Kristina      |
|  4039 |0               |Hello, I'm Kristina |April         |
|  4810 |0               |Hello, I'm April    |Bertha        |
|  4810 |0               |Hello, I'm April    |Kristina      |
|  4810 |0               |Hello, I'm April    |April         |

Which I didn't get at all, so I'm stuck.

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

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

发布评论

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

评论(1

只是一片海 2025-01-19 02:11:48

我不确定为什么您认为这里需要一个交叉连接,看来您需要一个简单的内部连接:

select m.author, m.is_public, m.content, c.first_name
from dapp.messages m 
join dapp.credentials c on c.id = m.author
where m.is_public = '0';

当您想要一个单列时,相关子查询也是一个可行的选择:

  select m.author, m.is_public, m.content, 
    (select first_name from dapp.credentials c where c.id = m.author) as first_name
  from dapp.messages m 
  where m.is_public = '0';

I'm not sure why you think you need a cross join here, it appears you need a simple inner join:

select m.author, m.is_public, m.content, c.first_name
from dapp.messages m 
join dapp.credentials c on c.id = m.author
where m.is_public = '0';

As you want a single column a correlated subquery is also a viable option:

  select m.author, m.is_public, m.content, 
    (select first_name from dapp.credentials c where c.id = m.author) as first_name
  from dapp.messages m 
  where m.is_public = '0';
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文