如果字段匹配则更新和删除

问题描述 投票:0回答:2

我明白,在没有任何工作要求的情况下提出问题有点粗鲁,但是我有点难过,我需要一些帮助。

如果另一个表中的另一个字段等于1,我需要从mysql表中删除一行

我有两张桌子table_1table_2

table_1有两个领域locationevents

table_2有一个名为location的领域,与location上的table_1相同

如果字段table_2locationtable_1 = 1相同,我想从events中删除一行或多行

就像是:

if `events` = 1 in `table_1` in 'location' `*`;
    delete row(s) from `table_2`
    where `location` is the same as `location` in `table_1`
php mysql
2个回答
1
投票
DELETE t2
  FROM `table2` t2
  JOIN `table1` t1 ON t1.events = 1
                  AND t1.location = t2.location

3
投票

试试这个:

delete from `table_2` 
   where location in (select location from `table_1` where events=1)

编辑

DELETE t2 FROM `table_2` t2 
   JOIN `table_1` t1 ON t1.location = t2.location where t1.events=1
© www.soinside.com 2019 - 2024. All rights reserved.