2012-08-15 43 views

回答

2
select 
    t1.* , 
    t2.image 
from properties as t1 
left join (
      select 
       min(id), 
       pid, 
       image 
      from property_images 
      group by pid) as t2 
on t2.pid = t1.pid 
+0

您需要選擇PID如果你有by子句 – Rocky 2012-08-15 07:19:56

+0

把它列入你的小組謝謝raheel山 – Joyal 2012-08-15 07:23:40

+0

謝謝洛基提醒我在趕時間較早公佈也 – 2012-08-15 07:35:14

0
select * from property a 
left join (select * from property_Images b group by pid) s on s.pid=a.pid ; 
0
select 
    pro.* 
from properties as pro 
left join (
      select 
       min(id), 
       pid, 
       image 
      from property_images 
      group by pid) as pro_img 
on pro.pid = pro_img.pid 
0
select * 
from properties as t1 
left join property_images t2 on (t1.pid=t2.pid) 
where t2.id in 
      (select min(id) from property_images where property_images.pid=t2.pid) 

OR假設有在property_images一個唯一的密鑰ID:

select * 
from properties as t1 
left join (select min(id) minID, pid from property_images group by pid) t2 
    on (t1.pid=t2.pid) 
left join property_images t3 on (t2.minID=t3.id) 

選擇哪一個是你的數據庫更快。

相關問題