mysql - 如果子表的记录字段有值,如何更新字段?

标签 mysql sql-update

如果表 2 的字段之一和与表 1 相同的 idA 有一些 did=yes,我想更新表 1,字段 did 为 yes,否则 did=false。

我已经完成了一个触发器来更新新的更新,但现在我必须更新表中已有的所有旧记录。

表 1

idA b  done
--------------
 1  a   
 2  b
 3  c

表 2

idB idA done
--------------
 1   1   yes
 2   1   no
 3   1   yes
 4   2   no
 5   2   no
 6   3   no
 7   3   yes

http://sqlfiddle.com/#!2/8ee5f3/5

最佳答案

试试这个:

UPDATE table1 JOIN table2
ON table1.idA = table2.idA
AND table2.done = yes
SET table1.done = yes

工作演示:http://sqlfiddle.com/#!2/6066f/1

关于mysql - 如果子表的记录字段有值,如何更新字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21901751/

相关文章:

php - 更新查询在 codeigniter 中不起作用?

c# - 如何让我的 C# 和 MySql 从数据库返回多个条目?

mysql - 在mysql中编写存储过程时遇到locate问题

php - 日期 pdo 之间的日期时间

php - 我的更新查询出了什么问题

oracle - Oracle 中同一表不同行的多列更新,列非空 : Receiving error 01407

c# - 在 C# 中执行 sql 更新命令后没有任何反应。我的错误在哪里?

php - 将 SELECT @ 变量从 SQL 发送到 PHP

MySQL新手: Query to find the name of each employee whose salary exceeds the average salary of all employees in his or her department

mysql - 使用同一表中另一行的数据更新行