MySQL从两个表中删除

时间:2016-08-10 20:24:20

标签: mysql sql

我有两张桌子, 第一个表(table1):

| Field    | Type       | Null | Key | Default | Extra          |
+----------+------------+------+-----+---------+----------------+
| id       | bigint(20) | NO   | PRI | NULL    | auto_increment |
| user_id  | int(11)    | NO   | MUL | NULL    |                |
| who_id   | int(11)    | NO   | MUL | NULL    |                |
| ts       | int(11)    | NO   | MUL | NULL    |                |
+----------+------------+------+-----+---------+----------------+

第二张表(表2):

+----------+------------+------+-----+---------+----------------+
| Field    | Type       | Null | Key | Default | Extra          |
+----------+------------+------+-----+---------+----------------+
| id       | bigint(20) | NO   | PRI | NULL    | auto_increment |
| user_id  | int(11)    | NO   | MUL | NULL    |                |
| owner_id | int(11)    | NO   | MUL | NULL    |                |
| ts       | int(11)    | NO   |     | NULL    |                |
| was_read | tinyint(1) | NO   |     | NULL    |                |
+----------+------------+------+-----+---------+----------------+

我想按条件table1

在一个查询中删除table2WHERE table1.ts < UNIX_TIMESTAMP() - 30 * 24 * 60 * 60 AND table1.user_id = table2.user_id AND table1.who_id = table2.owner_id中的数据

需要与LIMIT类似的查询:

DELETE t1, t2 
FROM table1 t1, table2 t2 
WHERE t1.ts < UNIX_TIMESTAMP() - 30 * 24 * 60 * 60 
AND t1.user_id = t2.user_id AND t1.who_id = t2.owner_id;

列ID不相交

0 个答案:

没有答案