gpt4 book ai didi

mysql - 使用行值在 mysqli 中选择列

转载 作者:行者123 更新时间:2023-11-29 15:45:33 26 4
gpt4 key购买 nike

我在 mysqli 中有两个表:

table a:
cluster, id, number
_ _ _ _ _ _ _ _ _ _
c_a 1 135
c_a 2 155
c_a 3 187
c_b 1 232
c_b 2 984
c_b 3 213

table b:
user, c_a, c_b
_ _ _ _ _ _
alex 1 2
lia 3 1

我想要获取表 a 的行,其中簇和 id 对应于表 b 中特定用户的条目。例如。 Alex 在集群 a 中具有 id=1,在集群 b 中具有 id=2,因此对于 alex,我想要输出:

c_a   1   135
c_b 2 984

有没有一种方法可以做到这一点并且适用于约 50 个集群?

最佳答案

看来你需要工会

select 'c_a' cluster, b.c_a,  a.number
from table_b
inner join table_a on a.id = b.c_a and b.user = 1
union all
select 'c_b' , b.c_b, a.numbrer
from table_b
inner join table_a on a.id = b.c_ab and b.user = 1

关于mysql - 使用行值在 mysqli 中选择列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57095535/

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