如何正确地将 table 中通过 id 链接到 MySQL 上的另一个 table 的行添加?
How to correctly add the rows from a table that were linked by an id to another table on MySQL?
假设以下查询:
SELECT author, is_public, content
FROM dapp.messages WHERE is_public = '0'
得到以下 table:
| author| is_public | content |
-----------------------------------------------
| 3240 |0 |Hello, I'm Bertha |
| 4039 |0 |Hello, I'm Kristina |
| 4810 |0 |Hello, I'm April |
现在,在这种情况下,author
列中的数字链接到另一个名为 credentials
的 table 中的一行,这意味着:
3240
链接到 credentials
中的这一行 table:
| id | first_name | member_type |
----------------------------------------
| 3240 |Bertha | regular |
4039
链接到 credentials
中的这一行 table:
| id | first_name | member_type |
----------------------------------------
| 4039 |Kristina | regular |
4810
链接到 credentials
中的这一行 table:
| id | first_name | member_type |
----------------------------------------
| 4039 |April | regular |
所以,我想知道将 credentials
table 中的 first name
列添加到此 [ 中第一个查询获得的 table 的正确方法=51=],最终得到这样的输出:
| 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 |
我根本没有得到,所以我被卡住了。
我不确定为什么你认为你需要一个 cross 连接,看来你需要一个简单的内部连接:
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';
假设以下查询:
SELECT author, is_public, content
FROM dapp.messages WHERE is_public = '0'
得到以下 table:
| author| is_public | content |
-----------------------------------------------
| 3240 |0 |Hello, I'm Bertha |
| 4039 |0 |Hello, I'm Kristina |
| 4810 |0 |Hello, I'm April |
现在,在这种情况下,author
列中的数字链接到另一个名为 credentials
的 table 中的一行,这意味着:
3240
链接到 credentials
中的这一行 table:
| id | first_name | member_type |
----------------------------------------
| 3240 |Bertha | regular |
4039
链接到 credentials
中的这一行 table:
| id | first_name | member_type |
----------------------------------------
| 4039 |Kristina | regular |
4810
链接到 credentials
中的这一行 table:
| id | first_name | member_type |
----------------------------------------
| 4039 |April | regular |
所以,我想知道将 credentials
table 中的 first name
列添加到此 [ 中第一个查询获得的 table 的正确方法=51=],最终得到这样的输出:
| 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 |
我根本没有得到,所以我被卡住了。
我不确定为什么你认为你需要一个 cross 连接,看来你需要一个简单的内部连接:
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';