在三个表上使用联合命令 (mysql)
using union command on three tables (mysql)
我有以下内容:
tblOwner: owner_num,lname,fname (PK owner_num)
tblMarina_slip: owner_num, slip_id (PK slip_id, FK owner_num)
tblService_Requests: service_id, slip_id, description (PK service_id, FK slip_id)
我是 MySql 的新手,需要使用联合命令来获得
的输出
tblOwner.Owner_num, tblOwner.Lname, tblOwner.Fname, tblServiceRequests.service_id, tblServiceRequests.Description
我不知道如何使用"union"命令来获取所需的数据集,当这三个表之间的唯一连接是[=22中的外键时=] 和 tblService_Requests。
select col1,col2,col3 from <tablename> where <conditions>
union
select col1,col2,col3 from <tablename> where <conditions>
union
select col1,col2,col3 from <tablename> where <conditions>
注意:每个 table 中的 col1
、col2
、col3
必须相同。
您需要的是连接而不是联合:
SELECT t.owner_num,t.Lname,t.Fname,s.service_id,s.description
FROM tblOwner t
INNER JOIN tblMarina_slip p on(t.owner_num = p.owner_num)
INNER JOIN tblService_Requests s on(p.slip_id = s.slip_id)
联合用于将两个或多个输出组合在一起(仅在它们与列号和列类型相同的情况下)
当你想在同一行显示多个 table 信息时,你应该像我的回答一样使用 JOIN。
编辑:
如果要求使用union,可以这样欺骗数据:
SELECT max(owner_num),max(Lname),max(Fname),slip_id,max(description)
FROM
(SELECT owner_num,max(Lname) as Lname,max(Fname) as Fname, max(slip_id) as slip_id,'' as description
FROM(
SELECT owner_num,Lname,Fname,0 as slip_id from tblOwner
UNION
SELECT owner_num,'' as Lname,'' as Fname,slip_id from tblMarina_slip)
GROUP BY owner_num
UNION
SELECT 0 as owner_num, '' as Lname, '' as Fname,slip_id,description FROM tblService_Requests)
GROUP BY slip_id
您不需要联合命令:
select tow.Owner_num, tow.Lname, tow.Fname, ts.service_id, ts.Description
from tblOwner tow, tblServiceRequests ts, tblMarina_slip tm
where tow.owner_num = tm.owner_num
and tm.slip_id = ts.slip_id
我有以下内容:
tblOwner: owner_num,lname,fname (PK owner_num)
tblMarina_slip: owner_num, slip_id (PK slip_id, FK owner_num)
tblService_Requests: service_id, slip_id, description (PK service_id, FK slip_id)
我是 MySql 的新手,需要使用联合命令来获得
的输出tblOwner.Owner_num, tblOwner.Lname, tblOwner.Fname, tblServiceRequests.service_id, tblServiceRequests.Description
我不知道如何使用"union"命令来获取所需的数据集,当这三个表之间的唯一连接是[=22中的外键时=] 和 tblService_Requests。
select col1,col2,col3 from <tablename> where <conditions>
union
select col1,col2,col3 from <tablename> where <conditions>
union
select col1,col2,col3 from <tablename> where <conditions>
注意:每个 table 中的 col1
、col2
、col3
必须相同。
您需要的是连接而不是联合:
SELECT t.owner_num,t.Lname,t.Fname,s.service_id,s.description
FROM tblOwner t
INNER JOIN tblMarina_slip p on(t.owner_num = p.owner_num)
INNER JOIN tblService_Requests s on(p.slip_id = s.slip_id)
联合用于将两个或多个输出组合在一起(仅在它们与列号和列类型相同的情况下)
当你想在同一行显示多个 table 信息时,你应该像我的回答一样使用 JOIN。
编辑:
如果要求使用union,可以这样欺骗数据:
SELECT max(owner_num),max(Lname),max(Fname),slip_id,max(description)
FROM
(SELECT owner_num,max(Lname) as Lname,max(Fname) as Fname, max(slip_id) as slip_id,'' as description
FROM(
SELECT owner_num,Lname,Fname,0 as slip_id from tblOwner
UNION
SELECT owner_num,'' as Lname,'' as Fname,slip_id from tblMarina_slip)
GROUP BY owner_num
UNION
SELECT 0 as owner_num, '' as Lname, '' as Fname,slip_id,description FROM tblService_Requests)
GROUP BY slip_id
您不需要联合命令:
select tow.Owner_num, tow.Lname, tow.Fname, ts.service_id, ts.Description
from tblOwner tow, tblServiceRequests ts, tblMarina_slip tm
where tow.owner_num = tm.owner_num
and tm.slip_id = ts.slip_id