php数据库去重查询,sql,去重查询distinct

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就没法去重复的了

回复讨论(解决方案)

DISTINCT 作用于整行

你的3个表中只要存在一对多的情况就难说了

DISTINCT 作用于整行

你的3个表中只要存在一对多的情况就难说了

那该怎么写呢?我刚才也查了,一个表用distinct,多个表的时候就不用它了,我没看懂别人的说法

再套一个 selectselect 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

再套一个 selectselect 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错了

哦,漏了 select

..... from

( select `object_id`, t3.width as....

本文原创发布php中文网,转载请注明出处,感谢您的尊重!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值