sql,去重查询distinct

mengfk000 2014-11-20 04:00:26

select DISTINCT `object_id`, t3.width as width, t3.height as height,t3.ismark as ismark, t2.rtime as add_time,t1.add_author as add_author,t1.image_url as image_url,t1.id as id,t1.browse_real_cnt as browse_real_cnt
from comments as t2
left join ornamentations as t1 on t1.id = t2.object_id
left join ornamentation_images as t3 on t3.ornamentation_id = t1.id
where ( t2.uid = 1013 ) AND ( t1.enable = '0' )
ORDER BY t2.id desc LIMIT 0,5


select后面的条件,带t3正常,能去重复的,加了t2,t1就没法去重复的了
...全文
131 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
xuzuning 2014-11-20
  • 打赏
  • 举报
回复
哦,漏了 select ..... from (select `object_id`, t3.width as....
mengfk000 2014-11-20
  • 打赏
  • 举报
回复
引用 3 楼 xuzuning 的回复:
再套一个 select
select DISTINCT * from 
  (`object_id`, t3.width as width, t3.height as height,t3.ismark as ismark, t2.rtime as     add_time,t1.add_author as add_author,t1.image_url as image_url,t1.id as id,t1.browse_real_cnt as   browse_real_cnt 
  from comments as t2 
  left join ornamentations as t1 on t1.id = t2.object_id 
  left join ornamentation_images as t3 on t3.ornamentation_id = t1.id 
  where ( t2.uid = 1013 ) AND ( t1.enable = '0' ) 
  ORDER BY t2.id desc) T
 LIMIT 0,5
报错,报 from comments as t2 from错了
xuzuning 2014-11-20
  • 打赏
  • 举报
回复
再套一个 select
select DISTINCT * from 
  (`object_id`, t3.width as width, t3.height as height,t3.ismark as ismark, t2.rtime as     add_time,t1.add_author as add_author,t1.image_url as image_url,t1.id as id,t1.browse_real_cnt as   browse_real_cnt 
  from comments as t2 
  left join ornamentations as t1 on t1.id = t2.object_id 
  left join ornamentation_images as t3 on t3.ornamentation_id = t1.id 
  where ( t2.uid = 1013 ) AND ( t1.enable = '0' ) 
  ORDER BY t2.id desc) T
 LIMIT 0,5
mengfk000 2014-11-20
  • 打赏
  • 举报
回复
引用 1 楼 xuzuning 的回复:
DISTINCT 作用于整行 你的3个表中只要存在一对多的情况就难说了
那该怎么写呢?我刚才也查了,一个表用distinct,多个表的时候就不用它了,我没看懂别人的说法
xuzuning 2014-11-20
  • 打赏
  • 举报
回复
DISTINCT 作用于整行 你的3个表中只要存在一对多的情况就难说了

21,887

社区成员

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

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