小编i_t*_*art的帖子

在多个表上执行左连接时遇到问题

我正在将一些mysql查询转移到postgres,我遇到了一个无效的问题.

select (tons of stuff)
from trip_publication 

left join trip_collection AS "tc" on
tc.id = tp.collection_id

left join 
            trip_author ta1, (dies here)
            trip_person tp1,
            trip_institution tai1,
            trip_location tail1,
            trip_rank tr1
    ON
            tp.id = ta1.publication_id 
            AND tp1.id = ta1.person_id 
            AND ta1.order = 1 
            AND tai1.id = ta1.institution_id 
            AND tail1.id = tai1.location_id 
            AND ta1.rank_id = tr1.id
Run Code Online (Sandbox Code Playgroud)

查询似乎在"trip_author ta1"行上死亡,我在上面标记了它.实际的错误消息是:

   syntax error at or near ","
   LINE 77:   (trip_author ta1, trip_person tp1, ... 
Run Code Online (Sandbox Code Playgroud)

我浏览了文档,似乎是正确的.我到底错在了什么?任何反馈都将非常感激.

sql postgresql syntax-error

6
推荐指数
1
解决办法
6381
查看次数

MySQL IF /查询转换为PostgreSQL

我正在将一个站点从mysql迁移到PostgreSQL(到目前为止我真的很喜欢),但我对某个查询有疑问.请注意,这不是一个存储函数 - 这是一个在python脚本中手动传递的字符串(我知道这是一个可怕的做法,但它只适用于内部应用程序).在mysql中,查询的结构如下:

SELECT 
  name,
  SUM(IF(is_correct, 1, 0)) AS correct,
  SUM(IF(is_correct, 0, 1)) AS wrong
FROM
  k_p AS p JOIN
  k_v AS v ON (variable_id=v.id)
WHERE
  content_type_id=%s %s %s
GROUP BY
  v.id
ORDER BY sort_order
Run Code Online (Sandbox Code Playgroud)

is_correct只是一个布尔数据的列,可以是0或1.我意识到你不能像在mysql中那样在postgres中做直接的IF.该文档似乎表明,优选转换将使用case/when语句.我没有成功地尝试过.SUM/IF语句是什么让我失望 - 其余的查询应该没问题.任何建议将不胜感激!

奖励问题:如果其中一个查询包含以下内容,您会怎么做?

 SUM(IF(ISNULL(is_correct), 0, 1)) AS wrong
Run Code Online (Sandbox Code Playgroud)

mysql sql postgresql

2
推荐指数
1
解决办法
1241
查看次数

标签 统计

postgresql ×2

sql ×2

mysql ×1

syntax-error ×1