基于其他表存在的Mysql Update

时间:2011-08-02 07:05:36

标签: mysql sql

我正在试图找出如何基于另一个表中的列中是否存在值来批量更新mysql表。

e.g。伪代码:

if Table1.`col`=Table2.`col` then
  Update Table1.`status`=1

if table2.`col` exists in table1.`col`
  Update Table1.`status`=1

实现这一目标的最佳方式是什么?

3 个答案:

答案 0 :(得分:3)

试试这个 -

UPDATE table1 t1
  JOIN table2 t2
    ON t1.col = t2.col
SET t1.status = 1;

答案 1 :(得分:1)

表1

col  | status 
-------------

jaga |   0

kala |   0

表2

col   | status 
--------------

jaga  |   1

latha |   0

如果Table1。col = Table2。col //那么这一点就是fullfill jaga记录。 然后更新Table1。status = 1 //所以表1 jaga行状态想要在1中更新。

我是正确的吗?。

然后尝试

UPDATE Table1 AS t1, Table2 AS t2 SET t1.col = 1 WHERE t1.col  = t2.col

Happy Codings,

答案 2 :(得分:-1)

update t_checkout A
INNER JOIN t_target B on A.Media_ID = B.Media_ID
set A.status = 'R'
where A.Media_ID = 45
and exists (select * from t_target where B.Media_ID = 45 and status = 'R');

这里的45是硬编码的,但实际上这个值来自php参数。

相关问题