简体   繁体   English

将MySQL查询移植到PostgreSQL

[英]Port MySQL query to PostgreSQL

I'm migrating my MySQL databases to PostgreSQL. 我正在将MySQL数据库迁移到PostgreSQL。 While connecting database to my Python script I'm getting some syntax error. 将数据库连接到我的Python脚本时,出现一些语法错误。 So could you please convert this MySQL query into PostgreSQL: 因此,您能否将此MySQL查询转换为PostgreSQL:

select ifnull((select ifnull(run_id,0) from searchbank order by run_id desc limit 0,1),0)

Postgres doesn't support ifnull: Postgres不支持ifnull:

select coalesce((select coalesce(run_id,0)
                 from searchbank
                 order by run_id desc limit 0,1),0
               )

You might find this version slightly cleaner and clearer: 您可能会发现此版本稍微更清晰一些:

select coalesce(max(run_id), 0)
from searchbank

Simpler: 更简单:

SELECT COALESCE((SELECT max(run_id) FROM searchbank), 0);

The only point of a query like this is to return 0 instead of no row if searchbank has no rows. 这样的查询的唯一点是,如果searchbank 没有行,则返回0而不是返回行。

Removing the outer SELECT - like @Gordon demonstrates - cancels that effect and is strictly wrong. 删除外部SELECT (例如@Gordon演示)将取消该效果,并且完全是错误的。

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

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