springboot+mybatis多表关联查询

第一张表
app
id app_type
5 2

第二张表
id app_type app_name
1 0 A
2 0 B
3 0 C
4 1 D
5 2 E
6 3 F

输入参数为5,查询到app_type, 然后在第二张表中根据app_type 2查询他的app_name,
同时也要加上app_type为0的app_name,怎样查询?
怎样根据参数联表查询然后还要加上app_type为0的内容?

0是默认都加上的,如果是

 select  app_name from 表2 whre app_type = 0 or app_type in (select app_type from 表1 where id = 5)

select distinct tb2.app_name from tb1 left join tb2 on tb1.app_type=tb2.app_type where tb1.id=? or tb2.app_type= 0

?代表你传入的参数,distinct是为了去重,比如说,你传入的id是1,这时候就会查到2条一样数据,不建议楼上in方法,具体为什么可以百度表连接和in的效率

app_type如果存在索引,建议你用union all
select app_name from t2 whre app_type = 0 union all select t2.app_name from t1 inner join t2 on t1.app_type=t2.app_type where tb1.id=?