2015-12-29 65 views
0

我有兩個表:一個表PERSON和一個表CAR。兩張桌子之間有一對多的關係:一個人可以擁有多輛汽車,一輛汽車可以擁有一個人,並且只能擁有一個人。從兩個表中選擇列並將結果按特定順序排列在一個柱中

表PERSON已以下列:

PERSON 
      PERSON_ID 
      PERSON_NAME 

表CAR具有以下的列:

CAR 
      CAR_ID 
      CAR_DESCRIPTION 
      PERSON_ID (the owner) 

兩個表的內容如下:

PERSON 
    PERSON_ID PERSON_NAME 
    ------------------------- 
    1   John 
    2   Karl 
    3   Sarah 
    4   Kevin 
    -------------------------- 

    CAR 
    CAR_ID CAR_DESCRIPTION  PERSON_ID (owner) 
    ----------------------------------------- 
    1   Mercedes   3 
    2   Honda    3 
    3   Hundai    1 

我想用上面的表格得到類似於下面的表格結果:

Result: 
    john 
    Hundai 
    karl 
    sarah 
    mercedes 
    honda 
    kevin 

換句話說,我想要一個結果表格,由一列組成,其中顯示車主隨後是他擁有的所有車輛;那麼下一個人跟隨他擁有的汽車(如果有的話)等等。

這可以在數據庫級完成嗎?

+4

你確定你使用的是Oracle?您接受的答案將不會**與Oracle合作 –

回答

1

或者乾脆

select descr from (
select -1 as car_id, person_id, person_name as descr from person 
union all 
select car_id, person_id, car_description as descr from car 
order by 2,1) 
+1

更好的答案(並且實際上與Oracle接受的答案不同) –

0

可能的解決方法 -

with person as 
(
    select 1 as PERSON_ID, 'John' as PERSON_NAME from dual union all 
    select 2 as PERSON_ID, 'Karl' as PERSON_NAME from dual union all 
    select 3 as PERSON_ID, 'Sarah' as PERSON_NAME from dual union all 
    select 4 as PERSON_ID, 'Kevin' as PERSON_NAME from dual 
), 
car as 
(
    select 1 as CAR_ID, 'Mercedes' as CAR_DESCRIPTION, 3 as PERSON_ID from dual union all 
    select 2 as CAR_ID, 'Honda' as CAR_DESCRIPTION, 3 as PERSON_ID from dual union all 
    select 3 as CAR_ID, 'Hundai' as CAR_DESCRIPTION, 1 as PERSON_ID from dual 
) 
select lower(nm) as nm 
from 
    (select PERSON_ID as ID, null as PRN_ID, PERSON_NAME as NM 
     from person 
    union all 
    select CAR_ID as ID, PERSON_ID as PRN_ID, CAR_DESCRIPTION as NM 
     from car) 
order by nvl(prn_id, id) 

,並使用分層查詢 -

select lower(nm) as nm 
from 
    (select 1 as TP, PERSON_ID as ID, null as PRN_ID, PERSON_NAME as NM 
    from person 
    union all 
    select 2 as TP, CAR_ID as ID, PERSON_ID as PRN_ID, CAR_DESCRIPTION as NM 
    from car) 
connect by (prior id = prn_id) and ((prior tp = 1) and (tp = 2)) 
    start with prn_id is null 
    order siblings by id 
0

嘗試使用光標完成這個任務。

DECLARE @person_name NVARCHAR(150), 
     @last_person_name NVARCHAR(150), 
     @car_description NVARCHAR(150) 

DECLARE @result_tbl TABLE(value NVARCHAR(150)) 

DECLARE cur CURSOR FOR SELECT P.person_name, C.car_description FROM Person AS P 
          LEFT JOIN Car AS C ON P.person_id = C.person_id 

OPEN cur 
FETCH NEXT FROM cur INTO @person_name, @car_description 

WHILE @@FETCH_STATUS = 0 
BEGIN 
    IF @person_name <> ISNULL(@last_person_name, '') 
     INSERT INTO @result_tbl 
     VALUES(@person_name) 

    SET @last_person_name = @person_name 

    IF @car_description IS NOT NULL 
     INSERT INTO @result_tbl 
     VALUES(@car_description) 

    FETCH NEXT FROM cur INTO @person_name, @car_description 
END 

CLOSE cur 
DEALLOCATE cur 

SELECT * FROM @result_tbl 
+1

這對Oracle來說是無效的,而且太複雜了。這可以通過一個SQL查詢完成。 –

相關問題