join 一对多的时候查询重复数据问题

left join 一对多的时候会查询多条一样的吧

select a.id,a.category_name from search_category as a 
left join search_category_mode as b 
	on a.id=b.category_id 
where b.app_id = 1 and b.mode in (0,1) and a.tab_id = 2 and a.status = 1 order by sorts asc   

在这里插入图片描述

改用子查询解决重复的数据的问题

select * from search_category as a where
        exists (
            select 1 from search_category_mode 
            as b where a.id = b.category_id and a.status =1 and b.app_id = 1 ) 
        and a.tab_id  = 2  and a.status IN(0,1)  order by a.status,sorts

在这里插入图片描述

你可能感兴趣的:(MYSQL)