简体   繁体   English

如何更新 SQL 查询是/否?

[英]How do I update SQL query Yes/No?

I'm need to update sql table using if condition comparing another table.我需要使用 if 条件比较另一个表来更新 sql 表。 For example if phone numbers from 'a' table matches with 'b' table.例如,如果“a”表中的电话号码与“b”表匹配。 The new column 'flag' is updated to Y/N.新列“标志”更新为 Y/N。

Here's the code, it's working fine for 'Y' but how do I update the NULL or empty strings with 'N'这是代码,它适用于“Y”,但如何更新 NULL 或带有“N”的空字符串

Here's the code:这是代码:

update table_1 as a

set flag = 'Y'

from table_2 as b

where a.phone_numbers = b.phone_numbers

It is successfully working for 'Y' but how do I implement for 'N'它成功地为'Y'工作,但我如何为'N'实现

Thank you for your time ||感谢您的宝贵时间||

Use a LEFT join of table_1 to table_2 :使用table_1table_2LEFT连接:

UPDATE table_1 t1
LEFT JOIN table_2 t2 ON t2.phone_numbers = t1.phone_numbers
SET t1.flag = CASE WHEN t2.phone_numbers IS NOT NULL THEN 'Y' ELSE 'N' END;

I think this could work for you.我认为这对你有用。 All I've done is replace 'Y' with 'N' and change the = symbol to != , which means 'is not equal to'.我所做的只是将 'Y' 替换为 'N' 并将=符号更改为!= ,这意味着'不等于'。

Using these two queries you should get the results you want.使用这两个查询你应该得到你想要的结果。

update table_1 as a

set flag = 'N'

from table_2 as b

where a.phone_numbers != b.phone_numbers

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

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