如果值存在于另一个表中,如何更新 MYSQL 列? [英] How to update a MYSQL column if the value's exist in another table?

查看:43
本文介绍了如果值存在于另一个表中,如何更新 MYSQL 列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据库中有两个表,tableA 和 tableB.

I have a two tables in my database, tableA and tableB.

tableA:

id | col1  | col2  | in_b
-----------------------
1  | "abc" | "efg" | false
2  | "foo" | "bar" | false


tableB: 

id | col1  | col2  
------------------
1  | "abc" | "bar" 

我想更新 in_b 列,以便在 col1 或 col2 的值与 tableB 中的行匹配时为真.例如,两个 in_b 标志都为真,因为abc"在 tableB 的 col1 中,而bar"在 tableB 的 col2 中.col1 和 col2 不是唯一的.

I want to update the in_b column so that it is true if either col1 or col2's values matches a row in tableB. For example, both in_b flags would be true, because "abc" is in col1 of tableB and "bar" is in col2 of tableB. col1 and col2 are not unique.

更新 in_b 标志的最佳方法是什么?

What is the best way to update the in_b flag?

推荐答案

我想你想要一些类似

UPDATE tableA
SET in_b = TRUE
    WHERE EXISTS ( SELECT 1 FROM tableB
                   WHERE (tableB.col1 = tableA.col1 OR tableB.col2 = tableA.col2)
                 )

这篇关于如果值存在于另一个表中,如何更新 MYSQL 列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆