【SQL】特殊

SELECT 
    a.id, 
    a.name, 
    b.id AS b_id, 
    b.value
FROM 
    table_a a
LEFT JOIN 
    table_b b 
ON 
    b.a_id = a.id
AND 
    b.id = (
        SELECT b2.id 
        FROM table_b b2 
        WHERE b2.a_id = a.id 
        ORDER BY b2.id ASC
        LIMIT 1
    );
SELECT 
    a.id, 
    a.name, 
    b.id AS b_id, 
    b.value
FROM 
    table_a a
LEFT JOIN (
    SELECT 
        b.*, 
        ROW_NUMBER() OVER (PARTITION BY b.a_id ORDER BY b.id ASC) AS rn
    FROM 
        table_b b
) b ON a.id = b.a_id AND b.rn = 1;

是非フォローしてください

最新の情報をお伝えします