代码之家  ›  专栏  ›  技术社区  ›  ng5000

Oracle PL/SQL去噪结果

  •  1
  • ng5000  · 技术社区  · 15 年前

    提供三张表:一张汽车表、一张附加表和一张链接表,类似于:

    table_car
    ---------
    int car_id
    string make
    string model
    
    table_extras
    ------------
    int extra_id
    string extra
    
    table_car_extras_link
    ---------------------
    int car_id
    int extra_id
    

    我想编写一个pl/sql存储过程,它以以下方式返回数据:

    car_id, make, model, extra[]
    

    例如

    1, Ford, Fiesta, sunroof;electric windows
    2, BMW, M3, sports pack;alarm;sat nav
    3, subary, impreza, leather seats;ABS
    

    在数据库方面,我是一个新手,所以任何帮助都很感谢。请注意,在我们真正的系统中,我们将返回1000辆“汽车”,每辆车最多有10辆“额外的”。

    2 回复  |  直到 15 年前
        1
  •  4
  •   culebrón    15 年前

    这应该只是一个视图,不需要一个过程:

    create view myview as
    select c.car_id, c.make, c.model, WM_CONCAT(e.extra) as extras
    from table_car c, table_car_extras_link l, table_extras e
    where c.car_id=l.car_id and l.extra_id=e.extra_id
    group by c.car_id;
    

    wm_concat类似于字符串的和。

    this page 用于连接技术。

        2
  •  2
  •   Vincent Malgrat    15 年前

    以下将在9i及以上版本中工作,它使用 Tom Kyte's concatenation function :

    SELECT c.car_id, c.make, c.model, stragg(e.extra)
      FROM table_car c
      LEFT JOIN table_car_extras_link ce ON c.car_id = ce.car_id
      LEFT JOIN table_extras e ON ce.extra_id = e.extra_id
    GROUP BY c.car_id, c.make, c.model
    
    推荐文章