【问题标题】:sql presto query to join 2 tables interatablysql presto 查询以可交互方式连接 2 个表
【发布时间】:2019-01-09 03:16:10
【问题描述】:

我需要在 sql 查询中执行此操作。让我知道这是否可能

我有一个表,它的映射类似于 (table1)

num,value
2,'h'
3,'b'
5,'c'

现在我有另一个包含这些值的表 (table2)

name, config
"test1",45
"test2",20

现在我想要的是 sql 查询,它将通过检查配置列值是否可被 table1.num 整除,如果是,将 table1.values 连接到它,将另一列添加到我的 table2

所以现在在 sql 查询之后它应该变成

name, config, final
"test1",45, bc
"test2",20, hc

如果我可以对此进行查询,请告诉我

【问题讨论】:

  • 您使用的是 MySQL 还是 PrestoDB?这个问题被标记为两者,所以不清楚。

标签: mysql presto


【解决方案1】:

您可以使用交叉连接、mod 函数 https://dev.mysql.com/doc/refman/8.0/en/mathematical-functions.html#function_mod 和 group_concat https://dev.mysql.com/doc/refman/8.0/en/group-by-functions.html#function_group-concat

select t2.name,t2.config,group_concat(t1.value separator '') final
from table1 t1
cross join table2 t2
where t2.config % t1.num = 0
group by t2.name,t2.config

+-------+--------+-------+
| name  | config | final |
+-------+--------+-------+
| test1 |     45 | bc    |
| test2 |     20 | hc    |
+-------+--------+-------+
2 rows in set (0.00 sec)

【讨论】:

    【解决方案2】:

    P.Salmon 的答案应该适用于 MySQL。如果您使用的是 Presto,那么这将起作用:

    SELECT t2.name,
           t2.config,
           array_join(array_agg(t1.value),'','') AS final
    FROM table1 t1
    CROSS JOIN table2 t2
    WHERE t2.config % t1.num = 0
    GROUP BY t2.name,
             t2.config
    

    【讨论】:

      猜你喜欢
      • 2020-05-13
      • 2013-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多