sql - Postgresql 在 UPDATE 请求中添加 WHERE 子句

标签 sql postgresql

我打算写一个 SQL 请求 UPDATE,我想验证 table2.fld5 是否已经包含 'Hello' 为了不写几次,但没有找到实现此验证的方法。

请求是:

UPDATE table2
    SET fld5 = concat(fld5, 'Hello')
    WHERE NOT EXISTS (SELECT *
                      FROM table1
                      WHERE table1.fld1 = table2.fld1 AND
                            table1.fld2 = table2.fld2 AND
                            table1.fld3 = table2.fld3
                     )
     AND table2.fld4 > (NOW() - INTERVAL '79 MINUTE');

最佳答案

也许您的日期检查需要在现有子查询之外:

UPDATE table2 SET fld5 = concat(fld5, 'Hello') 
WHERE fld5 NOT LIKE '%Hello%' AND table2.fld4 > (NOW() - INTERVAL '79 MINUTE'
AND NOT EXISTS (SELECT * FROM table1 WHERE table1.fld1 = table2.fld1 AND table1.fld2 = table2.fld2 AND table1.fld3 = table2.fld3))

;

关于sql - Postgresql 在 UPDATE 请求中添加 WHERE 子句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41038919/

相关文章:

asp.net - SQL 中函数中的无效对象名称错误

mysql - 2 个数据库之间更快的 mysql 查询

postgresql - 本地计算机上的 postgresql-x64-9.6 服务启动然后停止。某些服务在不使用时会自动停止

python - 类不以多态方式加载

sql - 邮政系统;查询以处理找到的一组 ID

mysql - 公立学校系统的ER图

php - 我可以在更新中使用mysql的md5功能吗?

MySQL Unknown column in where clause but column exists

postgresql - 获取每个类别中的 N 个最新记录。 Postgres

string - 如何转换52 :34 string value to bigint