[英]selecting latest record from multiple table of each category
I have three tables *book_category, books and book_pictures* in my mysql. 我的mysql中有三个表* book_category,books和book_pictures * 。 i want to get latest book of each book category order by amended .
我想通过修改来获取每个书籍类别顺序的最新书籍。 I use this query to get latest books and it works fine.
我使用此查询来获取最新书籍,并且效果很好。
select * from (select * from books ORDER BY amended DESC) AS x GROUP BY book_sub_category_id
but i want join my book_pictures tables so i get book_pictures url to display book images. 但我想加入我的book_pictures表,以便获取book_pictures网址以显示图书图像。 how to write query to fetch latest books with its url from book_pictures table ?
如何编写查询以从book_pictures表中获取带有URL的最新书籍? below i show you the structure of the tables.
下面,我向您展示表格的结构。
book_category
+-------------+-----------------+
|category_id | category_name |
|-------------------------------|
| ca | custom Act |
| ct | Custom Tarif |
|-------------+-----------------+
books
+-------------+-----------------+-----------------+--------------------------+
| book_id | category_id | name | amended |
|-------------------------------|-----------------+--------------------------|
| 01 | ca | custom Act | 01-06-2011 |
| 02 | ca | custom Act | 01-06-2012 |
| 03 | ca | custom Act | 01-06-2013 |
| 04 | ct | custom tarif | 01-07-2011 |
| 05 | ct | custom tarif | 01-07-2012 |
| 06 | ct | custom tarif | 01-07-2013 |
+-------------+-----------------+-----------------+--------------------------+
book_pictures
+-------------+-----------------+-----------------+--------------------------+
| picture_id | book_id | small_url | large_url |
|-------------------------------|-----------------+--------------------------|
| p1 | 01 | url | url |
| p2 | 02 | url | url |
| p3 | 03 | url | url |
| p4 | 04 | url | url |
| p5 | 05 | url | url |
| p6 | 06 | url | url |
+-------------+-----------------+-----------------+--------------------------+
use left join, 使用左联接,
I think this will work, haven't run it tho. 我认为这会奏效,还没有运行。
SELECT * FROM (
SELECT b.*,
p.large_url
FROM books b
LEFT JOIN book_pictures p
ON b.book_id = p.book_id
ORDER BY b.amended DESC
) AS x
GROUP BY book_sub_category_id
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.