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

molaifeng
博客专家认证
2014-05-08 02:20:50
三张表

题目表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语句,不甚感激!!!
...全文
336 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
molaifeng 2014-05-08
  • 打赏
  • 举报
回复
用group by
妖__ING 2014-05-08
  • 打赏
  • 举报
回复
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;

21,886

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧