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....