🌼

MySQLでjoinをともなう構文

2024/01/04に公開

INSERT FROM JOIN する

INSERT INTO table1(c1,c2,c3,c4)
    SELECT t2.c1, t3.c2
    FROM table2 as t2 
    INNER JOIN table3 as t3
        ON t2.id = t3.id
    WHERE t2.c1 = t3.c1;

UPDATE JOIN する

UPDATE publishers as t1
INNER JOIN libraries as t2
ON t1.id = t2.publisher_id
SET t1.default = t2.default
WHERE t2.default = 1;

INSERT SELECTする

INSERT INTO tableA (ax, ay)
SELECT cx, cy FROM tableC
WHERE cid = '100';

UPDATE JOIN GROUP BY する

UPDATE persons as t1
INNER JOIN (
    SELECT person_id ,GROUP_CONCAT(name SEPARATOR ' ') as alias
    FROM personaliases
    GROUP BY person_id
) as t2
ON t1.id = t2.person_id
SET t1.aliases = t2.alias;

Discussion