如何正确地将通过 id 链接的表中的行添加到 MySQL 上的另一个表?
假设以下查询:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定为什么您认为这里需要一个交叉连接,看来您需要一个简单的内部连接:
当您想要一个单列时,相关子查询也是一个可行的选择:
I'm not sure why you think you need a cross join here, it appears you need a simple inner join:
As you want a single column a correlated subquery is also a viable option: