删除时加入

发布于 2024-11-10 15:19:23 字数 324 浏览 3 评论 0原文

我正在使用mysql:

  • btableA有tableB_id列
  • tableB有some_interestring_column_on_TableB

我想要(下面的伪sql):

delete from tableA 
 where the associated row in tableB (via tableB_id) has  
     some_interestring_column_on_TableB = 'interestingValue'

请帮我将伪sql翻译成真正的sql。

I'm working with mysql:

  • btableA has tableB_id column
  • tableB has some_interestring_column_on_TableB

I want (pseudo sql below):

delete from tableA 
 where the associated row in tableB (via tableB_id) has  
     some_interestring_column_on_TableB = 'interestingValue'

Please help me to translate the pseudo sql into real sql.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

凤舞天涯 2024-11-17 15:19:23

试试这个:

 DELETE TableA
 WHERE tableB_id IN (
    SELECT id FROM TableB 
    WHERE  interestring_column='pizza');

Try this:

 DELETE TableA
 WHERE tableB_id IN (
    SELECT id FROM TableB 
    WHERE  interestring_column='pizza');
自控 2024-11-17 15:19:23

MySQL 支持 DELETE 语句中的 JOIN,以及从一个表中的多个表中删除单个语句。以下内容只会从 TABLEA 中删除:

DELETE ta
  FROM TABLEA ta
  JOIN TABLEB tb ON b.id = a.tableb_id
                AND b.col = 'some value'

如果您想从两个表中删除,请使用:

DELETE ta, tb
  FROM TABLEA ta
  JOIN TABLEB tb ON b.id = a.tableb_id
                AND b.col = 'some value'

也就是说,这种支持在其他数据库中非常罕见 - 您必须使用 IN大多数情况下存在

MySQL supports JOINs in the DELETE statement, as well as deleting from multiple tables in a single statement. The following will only delete from TABLEA:

DELETE ta
  FROM TABLEA ta
  JOIN TABLEB tb ON b.id = a.tableb_id
                AND b.col = 'some value'

If you wanted to delete from both tables, use:

DELETE ta, tb
  FROM TABLEA ta
  JOIN TABLEB tb ON b.id = a.tableb_id
                AND b.col = 'some value'

That said, this support is very uncommon in other databases -- you'd have to use IN or EXISTS in most cases.

忘东忘西忘不掉你 2024-11-17 15:19:23

我不知道 mysql 语法,但我在想类似的东西(来自 mssql):

delete from tableA 
where tableA.tableB_id in 
   (select tableB.id from tableB 
   where tableB.some_interesting_column_on_TableB = 'interestingValue')

I don't know mysql syntax but I'm thinking something like (from mssql):

delete from tableA 
where tableA.tableB_id in 
   (select tableB.id from tableB 
   where tableB.some_interesting_column_on_TableB = 'interestingValue')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文