mysql - sql查询时如何动态更换连接表
阿神
阿神 2017-04-17 13:51:21
0
3
671

当type == 1时,course_id关联的时t_course_info 这张表

当type == 2时,course_id关联的是t_venue_course_info这张表

所以现在,我在查询的时候,如何才能根据type来判断关联哪张表呢?而且查询的时候会对关联表的数据进行筛选。

求大家帮忙!

阿神
阿神

闭关修行中......

reply all(3)
Peter_Zhu

mysql has a case when, please explore it yourself

PHPzhong
select * from course c
left join t_course_info tci
on tci.id = c.course_id and c.type = 1
left join t_venue_course_info tvci
on tcvi.id = c.course_id and c.type = 2
巴扎黑

Mybatis’ isEqual can implement this dynamic sql, but for these two businesses, wouldn’t it be better to just write two sql directly?

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template