我有两个表属性和 property_images
where properties.pid = property_images.pid
一个属性可以有多个图像,我必须选择所有属性和第一个属性图像
我有两个表属性和 property_images
where properties.pid = property_images.pid
一个属性可以有多个图像,我必须选择所有属性和第一个属性图像
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
select * from property a
left join ( select * from property_Images b group by pid) s on s.pid=a.pid ;
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
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)
选择哪一个在您的数据库上更快。