PostgreSQL多重连接问题

huangapple go评论58阅读模式
英文:

PostgreSQL multiple join issue

问题

ERROR: 第二个INNER JOIN 语法有问题。要在第一个INNER JOIN 结束后加一个分号,然后才能开始第二个INNER JOIN。以下是修正后的代码:

SELECT
  access.id,
  access.user_id,
  access.device_id,
  access.origin,
  access.creation_date
FROM
  table1 access
  INNER JOIN (
    SELECT
      device_id,
      MAX (creation_date) AS creation_date
    FROM
      table1
    GROUP BY
      device_id
  ) gaccess ON access.device_id = gaccess.device_id
  AND access.creation_date = gaccess.creation_date
  INNER JOIN 
    table2 AS jobinfo 
    ON gaccess.device_id = jobinfo.id;

希望这有帮助!

英文:

Iam trying to create query with multiple JOINS, but Im receiving error message regarding syntax on second JOIN.

ERROR: syntax error at or near "INNER"
LINE 19: INNER JOIN table2 AS jobinfo ON gaccess....
^
SQL state: 42601
Character: 418

Can you help me what am I doing wrong?

SELECT
  access.id,
  access.user_id,
  access.device_id,
  access.origin,
  access.creation_date
FROM
  table1 access
  INNER JOIN (
    SELECT
      device_id,
      MAX (creation_date) AS creation_date
    FROM
      table1
    GROUP BY
      device_id
  ) gaccess ON access.device_id = gaccess.device_id
  AND access.creation_date = gaccess.creation_date;
  INNER JOIN 
    table2 AS jobinfo 
    ON gaccess.device_id = jobinfo.id

答案1

得分: 2

你有一个问题 - 在倒数第二个 INNER JOIN 之前有一个 ; - 你需要移除它。

SELECT access.id,
       access.user_id,
       access.device_id,
       access.origin,
       access.creation_date
FROM table1 access
INNER JOIN 
(
    SELECT device_id
    	  ,MAX(creation_date) AS creation_date
    FROM table1
    GROUP BY device_id
) gaccess 
    ON access.device_id = gaccess.device_id
    AND access.creation_date = gaccess.creation_date
INNER JOIN table2 AS jobinfo 
    ON gaccess.device_id = jobinfo.id;
英文:

You have a type - before the last INNER JOIN there is ; - you need to remove it.

SELECT access.id,
       access.user_id,
       access.device_id,
       access.origin,
       access.creation_date
FROM table1 access
INNER JOIN 
(
    SELECT device_id
		  ,MAX(creation_date) AS creation_date
    FROM table1
    GROUP BY device_id
) gaccess 
	ON access.device_id = gaccess.device_id
	AND access.creation_date = gaccess.creation_date
INNER JOIN table2 AS jobinfo 
    ON gaccess.device_id = jobinfo.id;

huangapple
  • 本文由 发表于 2023年2月8日 15:37:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/75382606.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定