简体   繁体   中英

Get only one record from child table

Hi I am using the following query to select only one file from child table that has a matching id with the parent table but its giving error. please help

    $query = $this -> db -> query("select post.id AS PostID, post.*, 
post_files.* from post LEFT JOIN post_files ON post.category_id='1' AND 
post.id=(SELECT * FROM post_files WHERE post.id=post_files.post_id LIMIT 1)");

post is the table where main information of posts will be saved and all the files likes images will be saved in post_files table. So i want to get only one file from post_files.

You have error in sub query It should be

SELECT post.id AS PostID, post.*,MIN(post_files.id) as file_id FROM post LEFT JOIN post_files ON post.id=post_files.post_id GROUP BY post.id HAVVING post.category_id='1';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM