表 imgtitle
id title time conone
子表 imginfo
img_id fid miaoshu
查询条件 id = fid ,子表fid有相同的只取一条
结果为 id title time miaoshu 表imgtitle最新10条记录的值
我只会写 SELECT ` id ` , `title `,`time `, `miaoshu ` FROM `imgtitle`, `imginfo` where id=fid ORDER BY id DESC LIMIT 10 ,这个查询结果是子表imginfo fid有相同也都列出来了。
子表fid有相同的只取一条的sql语句怎么写?
请高人指教。谢谢!
3 个解决方案
#1
select * from `imgtitle`, `imginfo` where `imgtitle`.`id`=`miaoshu`.`fid` group by `miaoshu`.`fid` ORDER BY `imgtitle`.`id` DESC LIMIT 10
#2
SELECT a. id , a.title, a.time, b.miaoshu FROM `imgtitle` a
inner join
(
SELECT ` fid `,`miaoshu` from `imginfo` group by fid
) b
on a.id=b.fid ORDER BY a.id DESC LIMIT 10
#3
感谢两位热心人,我用了1楼的方法。谢谢!
#1
select * from `imgtitle`, `imginfo` where `imgtitle`.`id`=`miaoshu`.`fid` group by `miaoshu`.`fid` ORDER BY `imgtitle`.`id` DESC LIMIT 10
#2
SELECT a. id , a.title, a.time, b.miaoshu FROM `imgtitle` a
inner join
(
SELECT ` fid `,`miaoshu` from `imginfo` group by fid
) b
on a.id=b.fid ORDER BY a.id DESC LIMIT 10
#3
感谢两位热心人,我用了1楼的方法。谢谢!