具有多个select语句的复杂MySQL查询

具有多个select语句的复杂MySQL查询,mysql,select,multi-table,Mysql,Select,Multi Table,Mysql中有三个表链接在一起: 个人资料(ID、姓名、资料..) 联系人(ID、ProfileID、描述、作战需求文件) 地址(ID、ProfileID、desc、Ord) 现在,我需要从profile表中选择所有profile,其中带有联系人和地址中的“desc”字段,其中Ord=1。(这是一个搜索功能,我将在表中显示客户的姓名、主要联系人信息和主要地址 目前,我可以通过三个单独的SQL请求执行此操作: SELECT Name, ID FROM Profile WHERE name=”bla

Mysql中有三个表链接在一起:

个人资料(ID、姓名、资料..)

联系人(ID、ProfileID、描述、作战需求文件)

地址(ID、ProfileID、desc、Ord)

现在,我需要从profile表中选择所有profile,其中带有联系人和地址中的
“desc”
字段,其中Ord=1。(这是一个搜索功能,我将在表中显示客户的姓名、主要联系人信息和主要地址

目前,我可以通过三个单独的SQL请求执行此操作:

SELECT Name, ID FROM Profile WHERE name=”bla”
然后在foreach循环中,我将运行另外两个请求:

SELECT ProfileID, desc FROM Contact WHERE ProfileID=MyProfileID AND Ord=1
SELECT ProfileID, desc FROM Address WHERE ProfileID=MyProfileID AND Ord=1

我知道您可以在一个查询中执行多个
SELECT
,有没有办法将所有三个
SELECT
组合成一个查询?

您应该能够
连接
profile.id
上的表和其他表中的
profileid

如果您确定所有三个表中都存在
profileid
,则可以使用
内部联接
内部联接
返回所有表中匹配的行:

select p.id,
  p.name,
  c.desc ContactDesc,
  a.desc AddressDesc
from profile p
inner join contact c
  on p.id = c.profileid
inner join address a
  on p.id = a.profileid
where p.name = 'bla'
  and c.ord = 1
  and a.ord = 1
如果不确定是否有匹配的行,则可以使用
左联接

select p.id,
  p.name,
  c.desc ContactDesc,
  a.desc AddressDesc
from profile p
left join contact c
  on p.id = c.profileid
  and c.ord = 1
left join address a
  on p.id = a.profileid
  and a.ord = 1
where p.name = 'bla'

如果您需要学习
JOIN
语法的帮助,这里有一个很棒的方法,您应该能够
连接
profile.id
上的表和其他表中的
profileid

如果您确定所有三个表中都存在
profileid
,则可以使用
内部联接
内部联接
返回所有表中匹配的行:

select p.id,
  p.name,
  c.desc ContactDesc,
  a.desc AddressDesc
from profile p
inner join contact c
  on p.id = c.profileid
inner join address a
  on p.id = a.profileid
where p.name = 'bla'
  and c.ord = 1
  and a.ord = 1
如果不确定是否有匹配的行,则可以使用
左联接

select p.id,
  p.name,
  c.desc ContactDesc,
  a.desc AddressDesc
from profile p
left join contact c
  on p.id = c.profileid
  and c.ord = 1
left join address a
  on p.id = a.profileid
  and a.ord = 1
where p.name = 'bla'

如果您需要帮助学习
JOIN
语法,这里有一个很好的当
Profile
表中的
ID
在表上至少有一个匹配项时,下面的查询只会选择列:
Contact
Address
。如果其中一个或两个都可以为空,请使用
LEFT JOIN
,而不是
内部JOIN
因为
LEFT JOIN
显示左侧表中的所有记录,而不管它是否与其他表匹配

SELECT  a.*, 
        b.desc as BDESC,
        c.desc as CDESC
FROM    Profile a
        INNER JOIN Contact b
            ON a.ID = b.ProfileID
        INNER JOIN Address c
            ON a.ID = c.ProfileID
WHERE   b.ORD = 1 AND
        c.ORD = 1 AND
        a.Name = 'nameHERE'
左连接
版本:

SELECT  a.*, 
        b.desc as BDESC,
        c.desc as CDESC
FROM    Profile a
        INNER JOIN Contact b
            ON a.ID = b.ProfileID AND b.ORD = 1
        INNER JOIN Address c
            ON a.ID = c.ProfileID AND c.ORD = 1
WHERE   a.Name = 'nameHERE'
要进一步了解加入的更多信息,请访问以下链接:


下面的查询仅当
Profile
表中的
ID
在表上至少有一个匹配项时才选择列:
联系人
地址
。如果其中一个或两个都可为空,请使用
左连接
而不是
内部连接
,因为
左连接
显示来自左连接的所有记录d边桌,无论它是否与其他桌匹配

SELECT  a.*, 
        b.desc as BDESC,
        c.desc as CDESC
FROM    Profile a
        INNER JOIN Contact b
            ON a.ID = b.ProfileID
        INNER JOIN Address c
            ON a.ID = c.ProfileID
WHERE   b.ORD = 1 AND
        c.ORD = 1 AND
        a.Name = 'nameHERE'
左连接
版本:

SELECT  a.*, 
        b.desc as BDESC,
        c.desc as CDESC
FROM    Profile a
        INNER JOIN Contact b
            ON a.ID = b.ProfileID AND b.ORD = 1
        INNER JOIN Address c
            ON a.ID = c.ProfileID AND c.ORD = 1
WHERE   a.Name = 'nameHERE'
要进一步了解加入的更多信息,请访问以下链接:

我根据您的要求创建了:

下面的查询将从数据库中检索所有匹配的记录。其检索配置文件id、名称Stuff和联系人表的描述

select p.id,p.name,p.stauff,c.descr,a.descr from profile as p
 inner join contact as c on c.profileid=p.id
 inner join address as a on a.profileid=p.id
 where p.name="bla" and c.ord=1 and a.ord=1
我根据您的要求创建了:

下面的查询将从数据库中检索所有匹配的记录。其检索配置文件id、名称Stuff和联系人表的描述

select p.id,p.name,p.stauff,c.descr,a.descr from profile as p
 inner join contact as c on c.profileid=p.id
 inner join address as a on a.profileid=p.id
 where p.name="bla" and c.ord=1 and a.ord=1

非常感谢!1为解释,2为链接,你的信息和JW信息帮助我更好地理解SQL…(我喜欢理解和学习答案,而不仅仅是复制粘贴!!!!非常感谢!1为解释,2为链接,你的信息和JW信息帮助我更好地理解SQL。。。(我喜欢理解和学习答案,而不仅仅是复制粘贴!!!!嗨,JW,你可以看看给蓝脚留下的评论,谢谢你也适用于你!!!嗨,JW,你可以看看给蓝脚留下的评论,谢谢你也适用于你!!!