Select first record in a One-to-Many relation using left join

后端 未结 7 1263
予麋鹿
予麋鹿 2020-12-02 16:52

I\'m trying to join two tables using a left-join. And the result set has to include only the first record from the \"right\" joined table.

Lets say I have two tables

7条回答
  •  鱼传尺愫
    2020-12-02 17:12

    In Oracle you can do:

    WITH first_b AS (SELECT code, min(rowid) AS rid FROM b GROUP BY code)) 
    SELECT a.code, a.emp_no, b.city, b.county
    FROM a 
    INNER JOIN first_b 
     ON first_b.code = a.code
    INNER JOIN b
     ON b.rowid = first_b.rid
    

提交回复
热议问题