我试图用 MySQL 语句说这样的话:
IF
table1.fieldA = "SearchText" AND
table2.fieldB = "FindThis"
THEN
table2.fieldC = "Y"
我很确定那里一定有一个JOIN
,但我是新手,所以我不知道如何写它。
你可以这样做
SELECT yourfields
FROM table1 join table2 on table1.ID= table2.ID
WHERE CASE WHEN table1.fieldA = "SearchText" AND table2.fieldB = "FindThis" THEN table2.fieldC ELSE '' END = "Y"
你可能需要也可能不需要ELSE ''
MySQL
我最终做的是VIEW
为每组表创建一个。然后我UPDATE
对这两列做了一个(因为它们现在在同一个表中),如下所示:
UPDATE view_table
SET truehold = CONCAT('Y', truehold)
WHERE columnA ='BLAH' AND columnB='12345'
然后,在构建我的查询时,我可以说:
WHERE truehold NOT LIKE 'Y%'
因为有一些重复,这使得列说“YY”或“YYY”。
无论如何,谢谢大家让我走上正确的道路。
不是很清楚你的要求。对于连接表更新;
Update t2
Set t2.fieldC = "Y"
from table2 t2 join table1 t1
on t2.colM = t1.colN --join with appropriate columns
where t1.fieldA = "SearchText" and t2.fieldB = "FindThis"
and t2.fieldC is null -- if you need to update only nulls (as per comment)