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

时间:2011-05-04 09:43:39

标签: php mysql

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

如果另一个表中的另一个字段等于1

,我需要从mysql表中删除一行

我有两张表table_1table_2

table_1有两个字段locationevents

table_2有一个名为location的字段,与location上的table_1相同

如果字段table_2location相同且table_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`

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:1)

DELETE t2
  FROM `table2` t2
  JOIN `table1` t1 ON t1.events = 1
                  AND t1.location = t2.location