4 回答
TA贡献1877条经验 获得超6个赞
SELECT `name`,`car` FROM table_a a
INNER JOIN table_ab ab ON a.id = ab.id_a
INNER JOIN table_b b ON ab.id_b = b.id
PS:您也可以在没有任何连接的情况下执行此操作,在某些情况下,它更快更干净。
SELECT `name`,`car` FROM table_a a,table_b b, table_ab ab
WHERE a.id = ab.id_a AND ab.id_b = b.id
在本例中,DESCRIBE 显示了相同的结果,因此任一选项都适用。
TA贡献1777条经验 获得超3个赞
我想你想要joins 和聚合:
select a.name, group_concat(car) as cars
from ab join
a
on a.id = ab.id_a join
b
on b.id = ab.id_b
group by a.id, a.name;
TA贡献1847条经验 获得超7个赞
你需要两个加入
select a.name, b.car
from table_ab ab
inner join table_a a ON a.id = ab.id_a
inner join table_b b ON b.id = ab.id_b
TA贡献1936条经验 获得超6个赞
您必须加入表,按名称分组并group_concat()结合使用concat():
select concat(a.name, ' has ', group_concat(car separator ' and ')) col
from table_a a
inner join table_ab ab on ab.id_a = a.id
inner join table_b b on ab.id_b = b.id
group by a.id, a.name
请参阅。
结果:
| col |
| --------------------- |
| tom has car1 and car2 |
| max has car1 |
- 4 回答
- 0 关注
- 161 浏览
添加回答
举报