我突然提出一个问题。当我编辑表A中的数据时,需要检查表B中是否存在相同的数据,如果表B中存在数据,则忽略更新,如果数据为空或Null,则表A中的数据需要更新表 B 中的更新。通过以下查询,我几乎可以实现它,但问题是当数据存在于表 B 中时,它只是删除数据。当数据存在时,它实际上应该忽略它。我猜案件陈述有一个小问题。请帮助我。
$strSQLInsert2 = "UPDATE Table B
SET
`tender_intendername` = CASE WHEN `tender_intendername`='' or `tender_intendername` IS NULL
THEN '".$values["intendername1"]."' END,
`no_of_participants` = CASE WHEN `no_of_participants`='' Or `no_of_participants` IS NULL
THEN '".$values["no_of_participants"]."' END
WHERE tender_id=" . $values["tender_id"];
最佳答案
您必须将 ELSE
添加到 CASE
表达式:
UPDATE Table B
SET `tender_intendername` = CASE
WHEN `tender_intendername`='' or
`tender_intendername` IS NULL
THEN '".$values["intendername1"]."'
ELSE `tender_intendername` END,
`no_of_participants` = CASE
WHEN `no_of_participants`='' Or
`no_of_participants` IS NULL
THEN '".$values["no_of_participants"]."'
ELSE `no_of_participants` END
WHERE tender_id=" . $values["tender_id"];
ELSE
语句将每个字段设置为等于其自身,因此它保证当字段不为空且不为 NULL
时该字段不会更改。
关于如果数据为 Null 或使用 CASE 语句为空,则 MySQl 查询 UPDATE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37291603/