Home > Backend Development > PHP Tutorial > 两个表联合查询,子表fid有相同的只取一条,怎么写sql语句

两个表联合查询,子表fid有相同的只取一条,怎么写sql语句

WBOY
Release: 2016-06-13 13:05:04
Original
1278 people have browsed it

两个表联合查询,子表fid有相同的只取一条,如何写sql语句?
本帖最后由 dfwye 于 2012-11-09 19:45:23 编辑 两个表联合查询,子表fid有相同的只取一条,如何写sql语句?
表 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语句怎么写? 
请高人指教。谢谢! 
------最佳解决方案--------------------
select * from `imgtitle`,  `imginfo` where `imgtitle`.`id`=`miaoshu`.`fid` group by `miaoshu`.`fid` ORDER BY `imgtitle`.`id` DESC LIMIT 10
------其他解决方案--------------------

SELECT  a. id  ,  a.title, a.time, b.miaoshu  FROM  `imgtitle` a<br />
inner join<br />
 (<br />
     SELECT  ` fid `,`miaoshu` from `imginfo` group by fid<br />
 ) b<br />
on  a.id=b.fid  ORDER BY a.id DESC LIMIT 10
Copy after login
  
------其他解决方案--------------------
感谢两位热心人,我用了1楼的方法。谢谢!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template