微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

clickhouse :使用 IN 左连接

如何解决clickhouse :使用 IN 左连接

我希望基于两个条件执行左连接:

SELECT
  ...
FROM
   soMetable AS a
LEFT JOIN someothertable AS b ON a.some_id = b.some_id 
  AND b.other_id IN (1,2,3,4)

我收到错误

支持的语法:JOIN ON Expr([table.]column,...) = Expr([table.]column,...) [AND Expr([table.]column,...) = Expr( [table.]column,...) ...]```

似乎join的条件必须是=,不能是in

有什么想法吗?

解决方法

考虑将 IN 运算符移动到子查询中:

SELECT
    a.number,b.number
FROM numbers(8) AS a
LEFT JOIN 
(
    SELECT *
    FROM numbers(234)
    WHERE number IN (1,2,3,4)
) AS b USING (number)

/*
┌─number─┬─b.number─┐
│      0 │        0 │
│      1 │        1 │
│      2 │        2 │
│      3 │        3 │
│      4 │        4 │
│      5 │        0 │
│      6 │        0 │
│      7 │        0 │
└────────┴──────────┘
*/

SELECT
    a.number,4)
) AS b USING (number)
SETTINGS join_use_nulls = 1 /* 1 is 'JOIN behaves the same way as in standard SQL. The type of the corresponding field is converted to Nullable,and empty cells are filled with NULL.' */

/*
┌─number─┬─b.number─┐
│      0 │     ᴺᵁᴸᴸ │
│      1 │        1 │
│      2 │        2 │
│      3 │        3 │
│      4 │        4 │
│      5 │     ᴺᵁᴸᴸ │
│      6 │     ᴺᵁᴸᴸ │
│      7 │     ᴺᵁᴸᴸ │
└────────┴──────────┘
*/

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。