如果列值包含与另一个表中的列值类似的字符串,则删除表行

时间:2017-11-02 12:00:51

标签: mysql

我有两个不同的表, table_foo table_bar 。如果列包含 table_bar

中的字符串,我想删除 table_foo 中的行

table_bar

     bar_id      |    address    |  stat_id  |
-----------------+---------------+-----------+
  COMM_14.15.7   |     12345     |     1     |
  COMM_13.03.9   |     78543     |     2     |

table_foo

         comm_id         |    managed    |
-------------------------+---------------+
  COMM_11.21.6_enabled   |     true      |
  COMM_14.15.7_enabled   |     true      |
  COMM_13.03.9_enabled   |     true      |

在此示例中, table_foo 中的2行将被删除,因为bar_id值 COMM_14.15.7 处于comm_id值 COMM_14.15.7 _enabled。

到目前为止我尝试的是这个查询:

DELETE FROM table_foo tf
USING table_bar tn
WHERE fsj.comm_id LIKE tn.bar_id;

我使用了LIKE命令,但在这个例子中,它没有用。也没有语法错误。任何有关此建议和想法都受到高度赞赏。

1 个答案:

答案 0 :(得分:0)

您需要在LIKE表达式中使用通配符。例如,如果您要查找comm_idbar_id开头的匹配项,那么这样的内容应该对您有用:

DELETE table_foo.*
FROM table_foo
INNER JOIN table_bar on table_bar.comm_id LIKE concat(table_foo.bar_id,'%');
相关问题