2017-01-29 3 views
1

한 열은 기본적으로,이 두 쿼리가 하나가되고 싶어요 :여러 별칭, 나도 같은 열에 여러 개의 별칭을 제공하기 위해 노력하고

singlePeople marriedPeople 
------------- -------------- 
Bob Kelly  John SMith 
John Adams 
:

SELECT name AS singlePeople FROM People 
JOIN ID FROM Numbers 
ON People.ID=Numbers.ID 
WHERE People.isMarried=f; 


SELECT name AS marriedPeople FROM People 
JOIN ID FROM Numbers 
ON People.ID=Numbers.ID 
WHERE People.isMarried=t; 

내가 내 결과를 같이 할

+0

테이블이 같은 점은 무엇입니까? 왜 당신은'join'이 필요한지 내게 명확하지 않습니다. –

답변

0

선택 T1. * T2. * 사람들 FROM (SELECT 명과 singlePeople AS 는 People.ID = Numbers.ID Peopl 숫자가 FROM ID 가입 e.isMarried = F) T1 이너 1 = 1

ON) = People.ID Numbers.ID WHERE People.isMarried = t ON T2 번호를 FROM ID 가입들의 marriedPeople AS (SELECT 이름 참여
+0

이 작동하지 않습니다. – pokemongirl1234

1

충분합니까? 그렇지 않으면

SELECT (CASE WHEN p.isMarried THEN 'Married' ELSE 'Single' END) as which, 
     name 
FROM People p JOIN 
    Numbers n 
    ON p.ID = n.ID; 

, 당신은 변수와 함께이 작업을 수행 할 수 있습니다

select max(case when not ismarried then name end) as single, 
     max(case when ismarried then name end) as married 
from (select name, p.ismarried, 
      (@rn := if(@i = ismarried, @rn + 1, 
         if(@i := ismarried, 1, 1) 
         ) 
      ) as seqnum 
     from people p join 
      numbers n 
      on p.id = n.id cross join 
      (select @i := NULL, @rn := 0) params 
     order by ismarried 
    ) pn 
group by rn;