簡體   English   中英

垂直連接兩個sql語句

[英]joining two sql statements vertically

我想將以下兩個sql語句合並為一個,以便共有23行(第一個sql有23行,第二個sql有20行,這20行在每個sql中都有唯一的字段位置)在第一個sql中有h3dac值和2nd 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";
$sql ="select * from (sel....
    union  
    select * from (sel....   

您可以使用聯合操作,它將返回結果而不會重復。

如果兩個sql語句返回相同的數據類型列和相同數量的列,則可以使用並集。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM