Loading

数据库小技巧:使用distinct去除重复行

这里有2个表

问题question表

 

 

楼层question_floor表

 

 

 

需求:已知某用户的id为uid,实现“我的回复”功能,即查看我回复的所有问题

遇到的问题:如果直接对question表和question_floor表进行连接查询的话,即

SELECT question.qid qid, question.uid uid, " +
"question.ask_time, question.title, question.content, " +
"question.current_floor, question.credit, question.is_resolved " +
"FROM question, question_floor " +
"WHERE question_floor.uid = #{uid} " +
"AND question_floor.fid != 1 ORDER BY question.ask_time desc

当A多次回复帖子P时,A点击"我的回复"按钮,将会显示多条帖子P的记录。

解决方案:使用distinct

SELECT DISTINCT question.qid qid, question.uid uid, " +
"question.ask_time, question.title, question.content, " +
"question.current_floor, question.credit, question.is_resolved " +
"FROM question, question_floor " +
"WHERE question_floor.uid = #{uid} " +
"AND question_floor.fid != 1 ORDER BY question.ask_time desc

 

 

posted @ 2019-12-09 19:03  拾月凄辰  阅读(1491)  评论(0编辑  收藏  举报