gpt4 book ai didi

mysql - sql presto查询以交互方式连接2个表

转载 作者:行者123 更新时间:2023-11-29 10:07:29 25 4
gpt4 key购买 nike

我需要在 sql 查询中执行此操作。如果可能的话请告诉我

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

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

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

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

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

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

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

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

最佳答案

您可以通过使用交叉连接,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)

关于mysql - sql presto查询以交互方式连接2个表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51635169/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com