0

我的 mysql 中有三个表*book_category、books 和 book_pictures*。我想通过修改获得每个书籍类别顺序的最新书籍。我使用这个查询来获取最新的书籍,它工作正常。

select * from (select * from books ORDER BY amended DESC) AS x GROUP BY book_sub_category_id

但我想加入我的 book_pictures 表,所以我得到 book_pictures url 来显示书籍图像。如何编写查询以从 book_pictures 表中获取最新书籍及其 url?下面我向您展示表格的结构。

       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                 |
+-------------+-----------------+-----------------+--------------------------+
4

1 回答 1

1

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
于 2013-06-04T12:38:42.957 回答