MySQLで複数テーブルのデータを削除する

1.テーブルの作成
CREATE TABLE table1 (id int, username nvarchar(30));
CREATE TABLE table2 (id int);
CREATE TABLE table3 (id int);
CREATE TABLE table4 (id int);

INSERT INTO table1 VALUES (1, 'testuser’),(2, 'ctmuser’);
INSERT INTO table2 VALUES (1),(2);
INSERT INTO table3 VALUES (1),(2);
INSERT INTO table4 VALUES (1),(2);

SELECT COUNT(*) FROM table1;
2
SELECT COUNT(*) FROM table2;
2
SELECT COUNT(*) FROM table3;
2
SELECT COUNT(*) FROM table4;
2
2.MySQL複数のテーブルのデータを削除
DELETE t1, t2, t3, t4 FROM
table1 as t1
INNER JOIN table2 as t2 on t1.id = t2.id
INNER JOIN table3 as t3 on t1.id=t3.id
INNER JOIN table4 as t4 on t1.id=t4.id
WHERE t1.username=’testuser’ AND t1.id=’1′;

MySQL

Posted by arkgame