Php 是否可以使用一条查询语句
出于本示例的目的,我有两个表-夫妇表和人员表:Php 是否可以使用一条查询语句,php,mysql,Php,Mysql,出于本示例的目的,我有两个表-夫妇表和人员表: Persons Table ID PERSON 1 Bob 2 Frank 3 Sally 4 Jane Couples Table ID HUSBAND WIFE 1 2 3 2 1 4 我是否可以编写一条查询语句,从两个表中进行选择,并使其以查询结果生成的方式进行连接: Couple 1 = Frank and Sally Couple 2 = Bob and Jane 谢谢 不过,请注意,现在并不是所有的婚姻都是夫妻关系。配偶1
Persons Table
ID PERSON
1 Bob
2 Frank
3 Sally
4 Jane
Couples Table
ID HUSBAND WIFE
1 2 3
2 1 4
我是否可以编写一条查询语句,从两个表中进行选择,并使其以查询结果生成的方式进行连接:
Couple 1 = Frank and Sally
Couple 2 = Bob and Jane
谢谢
不过,请注意,现在并不是所有的婚姻都是夫妻关系。配偶1和配偶2可能更能证明未来
不过,请注意,现在并不是所有的婚姻都是夫妻关系。配偶1和配偶2可能更能证明未来。类似的事情
select m.id as husband_id, m.person as husband_name, f.id as wife_id, f.person as wife_name
from couples c
inner join persons m on m.id=c.husband
inner join persons f on f.id=c.wife
像这样的
select m.id as husband_id, m.person as husband_name, f.id as wife_id, f.person as wife_name
from couples c
inner join persons m on m.id=c.husband
inner join persons f on f.id=c.wife
SELECT 'Couple ' + c.id + ' ' + h.person + ' and ' + w.person
FROM Couples c
JOIN Persons h ON h.id = c.husband
JOIN Persons w ON w.id = c.wife