我在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

10-02 05:30
查看更多