垂直连接两个sql语句

时间:2016-10-13 07:59:57

标签: php mysql

我想将以下两个sql语句合并为一个,这样我总共有23行(第一个sql有23行,第二个有20行中的20行,每个sql中都有唯一的字段位置)和h3dac值在第一个sql和第二个sql出现在列中。

请做些什么工作?

$sql_u1="select * from (select location1.location as locs, location1.elevation as elvn,location1.group as groups, shiftdata.location as loc1,  shiftdata.date,  shiftdata.shift,  shiftdata.h3dac as h3dac1 from location1 inner join shiftdata on location1.location=shiftdata.location where shiftdata.unit= 1 ORDER BY shiftdata.date desc, shiftdata.shift desc, location1.loc_id limit 23) as a left JOIN  (select location as loc1, date as date2, shift as shift2, h3dac as h3dac2  from shiftdata where unit= 1 ORDER BY date desc, shift desc limit 23 offset 23) as b on a.locs=b.loc1 left join (select location as loc3, date as date3, shift as shift3, h3dac as h3dac3 from shiftdata where unit= 1 ORDER BY date desc, shift desc limit 23 offset 46) as c on a.locs=c.loc3";

$sql_u2="select * from (select location2.location as locs,location2.elevation as elvn, location2.group as groups, shiftdata.location as loc1,  shiftdata.date,  shiftdata.shift,  shiftdata.h3dac as h3dac1 from location2 inner join shiftdata on location2.location=shiftdata.location where shiftdata.unit= 2 ORDER BY shiftdata.date desc, shiftdata.shift desc, location2.loc_id limit 20) as a left JOIN  (select location as loc1, date as date2, shift as shift2, h3dac as h3dac2 from shiftdata where unit= 2 ORDER BY date desc, shift desc limit 20 offset 20) as b on a.locs=b.loc1 left join (select location as loc3, date as date3, shift as shift3, h3dac as h3dac3 from shiftdata where unit= 2 ORDER BY date desc, shift desc limit 20 offset 40) as c on a.locs=c.loc3";

2 个答案:

答案 0 :(得分:0)

$sql ="select * from (sel....
    union  
    select * from (sel....   

你可以使用联合操作,它会返回没有重复的结果。

答案 1 :(得分:0)

如果两个sql语句返回相同的数据类型列和相同数量的列,则可以使用union。

$ sql = $ sql_u1。 “联盟”。 $ sql_u2