简体   繁体   English

匹配2个SQL列if =,然后将另一个列更新1

[英]Match 2 sql columns if = then update a different column by 1

Im looking to see if there is a match between what someone submitted in one table to a different table in the database. 我正在寻找某人在一个表中提交的内容与数据库中另一个表之间是否存在匹配项。 I can't quite figure out how to set it up. 我不太清楚如何设置它。 What im trying for is 我正在努力的是

  IF tableA column A = tableB column B then table A column C = Column C + 1. 

I have tried the update method but that seems to not be working for me. 我尝试了更新方法,但似乎对我不起作用。 Any help would be great. 任何帮助都会很棒。 Thanks. 谢谢。

Generally it would be this: 通常是这样的:

UPDATE TABLE_A a JOIN TABLE_B b 
  ON a.join_col = b.join_col AND a.column_a = b.column_b
SET a.column_c = a.column_c + 1

The join_col value is likely the user_id so that you only update rows in TABLE_A where that same user has the same value in TABLE_B . join_col值可能是user_id,因此您只更新TABLE_A同一用户TABLE_A中具有相同值的TABLE_B

I guess you can do this in mysql: 我猜你可以在mysql中做到这一点:

UPDATE TableA a, TableB b 
SET a.ColumnC = ColumnC + 1 
WHERE a.ColumnA = b.ColumnB; 

如果这是你想要的

update tableA set colA=(select (case when b.colB=colA then colC+1 else colC end) from tableB b)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM