2016-10-13 3 views
0

다음 23 개의 SQL 문을 하나로 결합하여 23 개의 행 (첫 번째 SQL은 23 개의 행을 갖고 두 번째 행은 20 개의 행을가집니다. 각 20 개의 행은 각각 고유 한 필드 위치와 공통입니다. sql)은 h3dac 값을 첫 번째 sql과 두 번째 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"; 

답변

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

당신은 노조 작업을 사용할 수 있습니다, 그것은 중복없이 당신에게 결과를 반환합니다.

0

두 개의 sql 문이 동일한 데이터 형식 열과 같은 수의 열을 반환하면 유니온을 사용할 수 있습니다.

$ sql = $ sql_u1 "UNION". $ sql_u2

+0

은 $ sql_u1 끝에 공백이 없으므로 "UNION"이어야합니다. –

+1

고정되어 있습니다. 감사. –

관련 문제