SQL Select 声明:一个 Child - 两个 Parents (两个不同的FK)
SQL Select Statement: One Child - Two Parents (two different FK)
我遇到的情况最好通过以下场景来解释:
- 其中一条
Child
记录有两个 Parent
(母亲和父亲)
如果您稍后阅读此post考虑应用self-referencing tables
场景的选项,则没有必要,没有祖父或孙子。
关于parent是怎么定义的:
CREATE TABLE IF NOT EXISTS parent(
code varchar(3),
name varchar(10),
PRIMARY KEY(code)
)ENGINE=INNODB;
记录插入方式:
INSERT INTO parent(code,name) VALUES('001','Mary');
INSERT INTO parent(code,name) VALUES('002','Joseph');
INSERT INTO parent(code,name) VALUES('003','Adan');
INSERT INTO parent(code,name) VALUES('004','Eva');
INSERT INTO parent(code,name) VALUES('005','Ana');
INSERT INTO parent(code,name) VALUES('006','Elcana');
并且 select
查询按预期工作:
mysql> select * from parent;
+------+--------+
| code | name |
+------+--------+
| 001 | Mary |
| 002 | Joseph |
| 003 | Adan |
| 004 | Eva |
| 005 | Ana |
| 006 | Elcana |
+------+--------+
6 rows in set (0.01 sec)
关于child是怎么定义的:
CREATE TABLE IF NOT EXISTS child(
code varchar(3),
name varchar(10),
PRIMARY KEY(code),
mother_code varchar(3),
father_code varchar(3),
FOREIGN KEY fk_mother_code(mother_code) REFERENCES parent(code),
FOREIGN KEY fk_father_code(father_code) REFERENCES parent(code)
)ENGINE=INNODB;
观察: 从上方观察 Child
期望 两个 PK
来自 Parent
(假设必须不同)通过两个 FK
s.
记录插入方式:
INSERT INTO child(code, name, mother_code, father_code) VALUES('001','Jesus', '001', '002');
INSERT INTO child(code, name, mother_code, father_code) VALUES('002','Cain', '003', '004');
INSERT INTO child(code, name, mother_code, father_code) VALUES('003','Abel', '003', '004');
INSERT INTO child(code, name, mother_code, father_code) VALUES('004','Set', '003', '004');
INSERT INTO child(code, name, mother_code, father_code) VALUES('005','Samuel', '005', '006');
并且 select
查询按预期工作:
mysql> select * from child;
+------+--------+-------------+-------------+
| code | name | mother_code | father_code |
+------+--------+-------------+-------------+
| 001 | Jesus | 001 | 002 |
| 002 | Cain | 003 | 004 |
| 003 | Abel | 003 | 004 |
| 004 | Set | 003 | 004 |
| 005 | Samuel | 005 | 006 |
+------+--------+-------------+-------------+
5 rows in set (0.00 sec)
目标是获得以下内容:
+------+--------+------+-------+-------------+-------------+
| code | name | code | name | mother_code | father_code |
+------+--------+------+-------+-------------+-------------+
| 001 | Mary | 001 | Jesus | 001 | 002 |
| 002 | Joseph | 001 | Jesus | 001 | 002 |
+------+--------+------+-------+-------------+-------------+
我试过以下方法:
SELECT p.*, c.* FROM parent p,
child c,
(SELECT pm.code AS m_code FROM parent pm) AS m,
(SELECT pf.code AS f_code FROM parent pf) AS f
WHERE
m.m_code='001' AND
f.f_code='002' AND
c.mother_code=m.m_code AND
c.father_code=f.f_code AND
c.mother_code='001' AND
c.father_code='002' AND
c.code='001';
where
子句看起来多余,这是因为我试图获得所需的结果,因此它包含尝试编写正确的查询。
但总是returns:
+------+--------+------+-------+-------------+-------------+
| code | name | code | name | mother_code | father_code |
+------+--------+------+-------+-------------+-------------+
| 001 | Mary | 001 | Jesus | 001 | 002 |
| 002 | Joseph | 001 | Jesus | 001 | 002 |
| 003 | Adan | 001 | Jesus | 001 | 002 |
| 004 | Eva | 001 | Jesus | 001 | 002 |
| 005 | Ana | 001 | Jesus | 001 | 002 |
| 006 | Elcana | 001 | Jesus | 001 | 002 |
+------+--------+------+-------+-------------+-------------+
6 rows in set (0.00 sec)
那么正确的句子是什么?
您是要找两个 join
吗?
select c.*, pm.name as mother_name, pf.name as father_name
from child c join
parent pm
on c.mother_code = pm.code join
parent pf
on c.father_code = pf.code;
您可以添加一个 where
子句来将其过滤到特定的 children:
where c.code in ('001', '002')
根据您的预期结果,我认为这就是您需要的:
select
p.code, p.name,
c.code, c.name,
c.mother_code, c.father_code
from parent p
inner join child c
on c.mother_code = p.code or c.father_code = p.code
您可以使用 WHERE 子句添加您需要的任何条件。
见 demo.
结果:
| code | name | code | name | mother_code | father_code |
| ---- | ------ | ---- | ------ | ----------- | ----------- |
| 001 | Mary | 001 | Jesus | 001 | 002 |
| 002 | Joseph | 001 | Jesus | 001 | 002 |
| 003 | Adan | 002 | Cain | 003 | 004 |
| 004 | Eva | 002 | Cain | 003 | 004 |
| 003 | Adan | 003 | Abel | 003 | 004 |
| 004 | Eva | 003 | Abel | 003 | 004 |
| 003 | Adan | 004 | Set | 003 | 004 |
| 004 | Eva | 004 | Set | 003 | 004 |
| 005 | Ana | 005 | Samuel | 005 | 006 |
| 006 | Elcana | 005 | Samuel | 005 | 006 |
如果依赖于 child 以获取 parent,请使用 or 加入。
SELECT p.Code, p.[Name], c.Code, p.[Name], c.Mother_Code, c.Father_Code
FROM Parent p JOIN Child c ON c.Mother_Code = p.Code OR c.Father_Code = p.Code
WHERE c.name = 'Jesus'
如果依赖是找一个parent的children,那么改一下WHERE语句就可以了
SELECT p.Code, p.[Name], c.Code, p.[Name], c.Mother_Code, c.Father_Code
FROM Parent p JOIN Child c ON c.Mother_Code = p.Code OR c.Father_Code = p.Code
WHERE p.name IN ('Mary', 'Joseph')
我遇到的情况最好通过以下场景来解释:
- 其中一条
Child
记录有两个Parent
(母亲和父亲)
如果您稍后阅读此post考虑应用self-referencing tables
场景的选项,则没有必要,没有祖父或孙子。
关于parent是怎么定义的:
CREATE TABLE IF NOT EXISTS parent(
code varchar(3),
name varchar(10),
PRIMARY KEY(code)
)ENGINE=INNODB;
记录插入方式:
INSERT INTO parent(code,name) VALUES('001','Mary');
INSERT INTO parent(code,name) VALUES('002','Joseph');
INSERT INTO parent(code,name) VALUES('003','Adan');
INSERT INTO parent(code,name) VALUES('004','Eva');
INSERT INTO parent(code,name) VALUES('005','Ana');
INSERT INTO parent(code,name) VALUES('006','Elcana');
并且 select
查询按预期工作:
mysql> select * from parent;
+------+--------+
| code | name |
+------+--------+
| 001 | Mary |
| 002 | Joseph |
| 003 | Adan |
| 004 | Eva |
| 005 | Ana |
| 006 | Elcana |
+------+--------+
6 rows in set (0.01 sec)
关于child是怎么定义的:
CREATE TABLE IF NOT EXISTS child(
code varchar(3),
name varchar(10),
PRIMARY KEY(code),
mother_code varchar(3),
father_code varchar(3),
FOREIGN KEY fk_mother_code(mother_code) REFERENCES parent(code),
FOREIGN KEY fk_father_code(father_code) REFERENCES parent(code)
)ENGINE=INNODB;
观察: 从上方观察 Child
期望 两个 PK
来自 Parent
(假设必须不同)通过两个 FK
s.
记录插入方式:
INSERT INTO child(code, name, mother_code, father_code) VALUES('001','Jesus', '001', '002');
INSERT INTO child(code, name, mother_code, father_code) VALUES('002','Cain', '003', '004');
INSERT INTO child(code, name, mother_code, father_code) VALUES('003','Abel', '003', '004');
INSERT INTO child(code, name, mother_code, father_code) VALUES('004','Set', '003', '004');
INSERT INTO child(code, name, mother_code, father_code) VALUES('005','Samuel', '005', '006');
并且 select
查询按预期工作:
mysql> select * from child;
+------+--------+-------------+-------------+
| code | name | mother_code | father_code |
+------+--------+-------------+-------------+
| 001 | Jesus | 001 | 002 |
| 002 | Cain | 003 | 004 |
| 003 | Abel | 003 | 004 |
| 004 | Set | 003 | 004 |
| 005 | Samuel | 005 | 006 |
+------+--------+-------------+-------------+
5 rows in set (0.00 sec)
目标是获得以下内容:
+------+--------+------+-------+-------------+-------------+
| code | name | code | name | mother_code | father_code |
+------+--------+------+-------+-------------+-------------+
| 001 | Mary | 001 | Jesus | 001 | 002 |
| 002 | Joseph | 001 | Jesus | 001 | 002 |
+------+--------+------+-------+-------------+-------------+
我试过以下方法:
SELECT p.*, c.* FROM parent p,
child c,
(SELECT pm.code AS m_code FROM parent pm) AS m,
(SELECT pf.code AS f_code FROM parent pf) AS f
WHERE
m.m_code='001' AND
f.f_code='002' AND
c.mother_code=m.m_code AND
c.father_code=f.f_code AND
c.mother_code='001' AND
c.father_code='002' AND
c.code='001';
where
子句看起来多余,这是因为我试图获得所需的结果,因此它包含尝试编写正确的查询。
但总是returns:
+------+--------+------+-------+-------------+-------------+
| code | name | code | name | mother_code | father_code |
+------+--------+------+-------+-------------+-------------+
| 001 | Mary | 001 | Jesus | 001 | 002 |
| 002 | Joseph | 001 | Jesus | 001 | 002 |
| 003 | Adan | 001 | Jesus | 001 | 002 |
| 004 | Eva | 001 | Jesus | 001 | 002 |
| 005 | Ana | 001 | Jesus | 001 | 002 |
| 006 | Elcana | 001 | Jesus | 001 | 002 |
+------+--------+------+-------+-------------+-------------+
6 rows in set (0.00 sec)
那么正确的句子是什么?
您是要找两个 join
吗?
select c.*, pm.name as mother_name, pf.name as father_name
from child c join
parent pm
on c.mother_code = pm.code join
parent pf
on c.father_code = pf.code;
您可以添加一个 where
子句来将其过滤到特定的 children:
where c.code in ('001', '002')
根据您的预期结果,我认为这就是您需要的:
select
p.code, p.name,
c.code, c.name,
c.mother_code, c.father_code
from parent p
inner join child c
on c.mother_code = p.code or c.father_code = p.code
您可以使用 WHERE 子句添加您需要的任何条件。
见 demo.
结果:
| code | name | code | name | mother_code | father_code |
| ---- | ------ | ---- | ------ | ----------- | ----------- |
| 001 | Mary | 001 | Jesus | 001 | 002 |
| 002 | Joseph | 001 | Jesus | 001 | 002 |
| 003 | Adan | 002 | Cain | 003 | 004 |
| 004 | Eva | 002 | Cain | 003 | 004 |
| 003 | Adan | 003 | Abel | 003 | 004 |
| 004 | Eva | 003 | Abel | 003 | 004 |
| 003 | Adan | 004 | Set | 003 | 004 |
| 004 | Eva | 004 | Set | 003 | 004 |
| 005 | Ana | 005 | Samuel | 005 | 006 |
| 006 | Elcana | 005 | Samuel | 005 | 006 |
如果依赖于 child 以获取 parent,请使用 or 加入。
SELECT p.Code, p.[Name], c.Code, p.[Name], c.Mother_Code, c.Father_Code
FROM Parent p JOIN Child c ON c.Mother_Code = p.Code OR c.Father_Code = p.Code
WHERE c.name = 'Jesus'
如果依赖是找一个parent的children,那么改一下WHERE语句就可以了
SELECT p.Code, p.[Name], c.Code, p.[Name], c.Mother_Code, c.Father_Code
FROM Parent p JOIN Child c ON c.Mother_Code = p.Code OR c.Father_Code = p.Code
WHERE p.name IN ('Mary', 'Joseph')