比较表与Postgres中的数组

时间:2020-03-06 00:37:51

标签: sql node.js postgresql outer-join

我需要将我的数组与一个表进行比较。

我需要知道数组中存在哪些代码,表中缺少哪些代码。 以及表中存在哪些代码,数组中缺少哪些代码。

我正在使用Node + KnexJS + PostgreSQL:

const myArray = `array['10001517','1509','1524','155400X','903B','910','1009201X']`
let divergence = await app.db.raw(`
    select file, t.code as acc_missing
    from unnest(${myArray}) file full join
        table_a as t 
        on t.code LIKE file
    where t.code is null or file is NULL
    AND t.version LIKE '010'
    AND t.inst = 300
`)

const errorDivergence = divergence.rows

我当前的查询会执行此操作,但是会产生以下错误:

(node:26060) UnhandledPromiseRejectionWarning:
error: FULL JOIN is only supported with merge-joinable or hash-joinable join conditions

1 个答案:

答案 0 :(得分:1)

就像错误消息所示,Postgres仅支持jobs = [ 2, 4, 6, etc... ] worker_count = 2 jobs.sortDescending() # O(n log n) # if there's 50 jobs and 2 workers, then take the first 25 jobs and sum them... # ...that's an upper_bound for the time required to complete all tasks by 2 workers, as it assumes that 1 unlucky worker will get all of the longest tasks upper_bound = jobs.take( jobs.count / worker_count ).sum() 的连接条件,该条件可用于合并连接或哈希连接。 FULL [OUTER] JOIN没有资格。

这是一个已知的限制。对于这种罕见的情况,人们没有足够的兴趣来激发解决方案。参见:

但是,您的问题中没有任何内容表明您实际上需要t.code LIKE file

我需要知道数组中存在哪些代码,表中缺少哪些代码。以及表中存在哪些代码,数组中缺少哪些代码。

那将表示相等(LIKE)-正常工作:

=

格式化查询的方式表示您希望在SELECT file, t.code AS acc_missing FROM unnest('{10001517,1509,1524,155400X,903B,910,1009201X}'::text[]) file FULL join table_a t ON t.code = file -- ! WHERE t.code IS NULL OR file IS NULL AND t.version LIKE '010' AND t.inst = 300; 周围加上括号。 (t.code IS NULL OR file IS NULL)AND之前绑定。 The manual about operator precedence

OTOH,添加的谓词OR仅在没有括号的情况下才有意义。因此,这是一种使用AND t.version LIKE '010' AND t.inst = 300LEFT实现原始查询的解决方法:

RIGHT JOIN

或者:

SELECT file, t.code AS acc_missing
FROM   unnest('{10001517,1509,1524,155400X,903B,910,1009201X}'::text[]) file
LEFT   JOIN table_a t ON t.code LIKE file
WHERE  t.code IS NULL

UNION ALL
SELECT file, t.code AS acc_missing
FROM   unnest('{10001517,1509,1524,155400X,903B,910,1009201X}'::text[]) file
RIGHT  JOIN table_a t ON t.code LIKE file
WHERE  file IS NULL AND t.version LIKE '010' AND t.inst = 300;

db <>提琴here