MySQL左连接多个ID
我需要一些关于以下查询的帮助。MySQL左连接多个ID,mysql,join,left-join,Mysql,Join,Left Join,我需要一些关于以下查询的帮助。 我有下表,我正在尝试执行联接,但它没有按预期工作: TABLE1 Id1 | Title 1 | A 2 | B 3 | C TABLE2 Id2 | Id1 10 | 1 20 | 1 30 | 1,2 因此,表2中的Id1列基本上可以接受多于1个值。 我已开始使用以下SQL,但它没有按预期工作: select t1.id1,count(1) from table1 t1 left join table2
我有下表,我正在尝试执行联接,但它没有按预期工作:
TABLE1
Id1 | Title
1 | A
2 | B
3 | C
TABLE2
Id2 | Id1
10 | 1
20 | 1
30 | 1,2
因此,表2中的Id1列基本上可以接受多于1个值。
我已开始使用以下SQL,但它没有按预期工作:
select t1.id1,count(1)
from table1 t1
left join table2
on t1.id1 = t2.id2
group by t1.id1 desc;
我还尝试将t1.id1=t2.id2替换为:
-t2.id2中的t1.id1
-t1.id1类concat(“%”,t2.id2,“%”)
但它仍然没有像预期的那样发挥作用。
此查询应返回表1中的所有ID,并应统计表2中Id1的所有实例
有什么想法/建议吗?以下SQL正在返回我需要的数据:
select t1.id1,count(t2.id1)
from table1 t1
left join table2 t2
on find_in_set(t1.id1,t2.id1)
group by t1.id1 desc;
基本上,表2上的Id1列可以接受超过1个值,这从根本上说是有缺陷的