Maison  >  Questions et réponses  >  le corps du texte

mysql - sql查询时如何动态更换连接表

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

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

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

求大家帮忙!

阿神阿神2742 Il y a quelques jours618

répondre à tous(3)je répondrai

  • 天蓬老师

    天蓬老师2017-04-17 13:53:21

    mysql有case when 具体自己摸索下

    répondre
    0
  • PHPz

    PHPz2017-04-17 13:53:21

    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

    répondre
    0
  • 巴扎黑

    巴扎黑2017-04-17 13:53:21

    Mybatis的isEqual是可以实现这个动态sql,但是这是两个业务直接写两个sql不就好了吗

    répondre
    0
  • Annulerrépondre