>백엔드 개발 >PHP 튜토리얼 >sql,去重查询distinct

sql,去重查询distinct

WBOY
WBOY원래의
2016-06-23 13:44:483444검색

 

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,多个表的时候就不用它了,我没看懂别人的说法

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

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

哦,漏了 select
..... from
( select `object_id`, t3.width as....

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