>  기사  >  백엔드 개발  >  请教sql语句,多表查询一对多分页

请教sql语句,多表查询一对多分页

WBOY
WBOY원래의
2016-06-23 13:57:301163검색

    三张表

    题目表topic,字段 to_id to_title

     知识点表tag,字段t_id t_title

     关联表relation,字段to_id t_id

     每个题目可以有多个知识点


      现web界面提供搜索框,每页10个小题,用户可以多选知识点,遇到的问题是,有重复的数据的

select topic.to_id,topic.to_title from topic,relation where ( topic.to_id = relation.to_id )  and (t_id in (1,2,3)); 



    现在的情况是,如果知识点1,2,3都是对应to_id为1的,那么就会出现3道一样题目。可如果去掉重复的话,那么又和每页10条相悖了,特向前辈请教相关sql语句,不甚感激!!!


回复讨论(解决方案)

select distinct( topic.to_id,topic.to_title) from topic,relation ,tag where ( topic.to_id = relation.to_id )  and (t_id in (1,2,3)) limit 0,10;

用group by

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.