Mysql从一个表中按id从另一个表中删除

Mysql从一个表中按id从另一个表中删除,mysql,sql,Mysql,Sql,我有两张桌子: A id | name 及 在B表上,我创建查询来选择所有数据,其中id位于。。。一些值,但如何在一个查询中选择此B-id并从其中删除A.id=B.id 我是sql新手。。。。找不到如何在一个查询中合并所有查询 *在一些类似的查询中,我必须从b中选择,例如我的b查询:从b中选择*其中somefieldinteger在1,4,9,21,25中,并且该b.id必须与a进行比较* upd 这个抛出错误 delete `LINK_LA_TYP` FROM `LINK_LA_TYP` J

我有两张桌子:

A
id | name

在B表上,我创建查询来选择所有数据,其中id位于。。。一些值,但如何在一个查询中选择此B-id并从其中删除A.id=B.id

我是sql新手。。。。找不到如何在一个查询中合并所有查询

*在一些类似的查询中,我必须从b中选择,例如我的b查询:从b中选择*其中somefieldinteger在1,4,9,21,25中,并且该b.id必须与a进行比较*

upd 这个抛出错误

delete `LINK_LA_TYP` FROM `LINK_LA_TYP` JOIN `LINK_ART` ON `LINK_LA_TYP`.LAT_LA_ID=`LINK_ART`.LA_ID JOIN `ARTICLES` ON `LINK_ART`.LA_ART_ID=`ARTICLES`.ART_ID WHERE (`ARTICLES`.ART_SUP_ID in (10008,10439,11005,10097,10669,11100,80,10912,10683,10675,10194,11196,1166,10730,10248,10870,11200,11059,247,10121,10911,489,10724,496,10093,10205,1318,10953,11199,11047,128,114,194,10865,11058,10345,1286,10667,10064,11077,10622,11205,10917,10344,495,10709,10954,10744,304,10957,10447,10764,10129,10862,10918,10731,11115,10095,10859,10580,1345,10177,10323,144,11182,10132,256,10941,58,10006,10017,10780,10765,10665,11110,10714,10224,750,10267,10179,10725,10774,11063,10868,10103,10676,10057,10649,255,10322,11022,309,10754,11121,10801,10018,11004,10245,146,11056,381,10781,10699,11120,11126,830,10240,11162,10436,10584,10342,10861,11190,10721,11171,10564,10545,94,10087,73,10755,10869,10547,10706,10346,444,426,10059,153,122,10674,64,113,11101,10231,10337,806,11117,10385,251,11188,491,11192,100,10792,10069,10864,11099,10246,10178,10758,10568,10230,10124,10384,10782,10726,384,10670,305,10763,10768,10585,10394,10552,498,10677,1348,168,10814,10582,10382,11093,11173,10381,427,441)) limit 50
如果你漏掉了。。。和B.criteria=true它将删除A中出现在B中的所有行;否则,您将删除符合条件的内容。

将delete与join一起使用,如下所示:

mysql> create table a (id int);
mysql> insert into a values (1), (2), (3), (4);
mysql> create table b (id int);
mysql> insert into b values (2), (3);
mysql> delete a from a join b on a.id=b.id where b.id > 2;
mysql> select * from a;
+------+
| id   |
+------+
|    1 |
|    2 |
|    4 |
+------+
DELETE a 
FROM a 
JOIN b ON a.idA=b.idA 
JOIN c ON b.idB=c.idB
WHERE a.status='done' AND
      b.status='open' AND
      c.status='open';
DELETE a 
FROM a 
JOIN b ON a.idA=b.idA 
JOIN c ON b.idB=c.idB
LIMIT 500000;
DELETE a 
FROM a 
JOIN b ON a.idA=b.idA 
JOIN c ON b.idB=c.idB
ORDER BY a.something
LIMIT 500000;
这可扩展到任意数量的表,例如:

DELETE a 
FROM a 
JOIN b ON a.idA=b.idA 
JOIN c ON b.idB=c.idB;
这将从a中删除从b的记录中引用的所有记录,而b的记录又从c中引用。您还可以添加类似以下内容的WHERE子句:

mysql> create table a (id int);
mysql> insert into a values (1), (2), (3), (4);
mysql> create table b (id int);
mysql> insert into b values (2), (3);
mysql> delete a from a join b on a.id=b.id where b.id > 2;
mysql> select * from a;
+------+
| id   |
+------+
|    1 |
|    2 |
|    4 |
+------+
DELETE a 
FROM a 
JOIN b ON a.idA=b.idA 
JOIN c ON b.idB=c.idB
WHERE a.status='done' AND
      b.status='open' AND
      c.status='open';
DELETE a 
FROM a 
JOIN b ON a.idA=b.idA 
JOIN c ON b.idB=c.idB
LIMIT 500000;
DELETE a 
FROM a 
JOIN b ON a.idA=b.idA 
JOIN c ON b.idB=c.idB
ORDER BY a.something
LIMIT 500000;
如果要限制要删除的行数,请执行以下操作:

mysql> create table a (id int);
mysql> insert into a values (1), (2), (3), (4);
mysql> create table b (id int);
mysql> insert into b values (2), (3);
mysql> delete a from a join b on a.id=b.id where b.id > 2;
mysql> select * from a;
+------+
| id   |
+------+
|    1 |
|    2 |
|    4 |
+------+
DELETE a 
FROM a 
JOIN b ON a.idA=b.idA 
JOIN c ON b.idB=c.idB
WHERE a.status='done' AND
      b.status='open' AND
      c.status='open';
DELETE a 
FROM a 
JOIN b ON a.idA=b.idA 
JOIN c ON b.idB=c.idB
LIMIT 500000;
DELETE a 
FROM a 
JOIN b ON a.idA=b.idA 
JOIN c ON b.idB=c.idB
ORDER BY a.something
LIMIT 500000;
如果要删除前500000行,则需要细化哪些行是第一行,因此需要在行之间建立一些顺序。换句话说,您需要按某些标准对行进行排序,然后按如下方式进行限制:

mysql> create table a (id int);
mysql> insert into a values (1), (2), (3), (4);
mysql> create table b (id int);
mysql> insert into b values (2), (3);
mysql> delete a from a join b on a.id=b.id where b.id > 2;
mysql> select * from a;
+------+
| id   |
+------+
|    1 |
|    2 |
|    4 |
+------+
DELETE a 
FROM a 
JOIN b ON a.idA=b.idA 
JOIN c ON b.idB=c.idB
WHERE a.status='done' AND
      b.status='open' AND
      c.status='open';
DELETE a 
FROM a 
JOIN b ON a.idA=b.idA 
JOIN c ON b.idB=c.idB
LIMIT 500000;
DELETE a 
FROM a 
JOIN b ON a.idA=b.idA 
JOIN c ON b.idB=c.idB
ORDER BY a.something
LIMIT 500000;

这张桌子还没有relations@ValdisAzamaris你所说的关系是什么意思?在我的例子中,a和b之间没有明确的关系!a、 id=b.id很好,但首先我必须在一些类似的查询中从b中选择,例如我的b查询:select*from b,其中somefieldinteger在1,4,9,21,25中,并且b.id必须与之比较a@ValdisAzamaris添加了如何执行此操作的部分为什么b.id>2?也许聊天。俄语