1

我有两个表属性和 property_images

where properties.pid = property_images.pid

一个属性可以有多个图像,我必须选择所有属性和第一个属性图像

4

4 回答 4

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
于 2012-08-15T06:53:25.980 回答
0
select * from property a 
left join ( select * from property_Images b group by pid) s on s.pid=a.pid ;
于 2012-08-15T06:55:46.493 回答
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
于 2012-08-15T07:21:20.340 回答
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)

或者假设 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) 

选择哪一个在您的数据库上更快。

于 2012-08-15T11:05:23.837 回答