连接表上的限制和偏移量

时间:2014-04-22 07:21:13

标签: sql join limit

我有2个表类别项目,我想按照分类ID和limt和偏移量按降序显示

类别表:

  a   |  b  
 -----+-----
    1 | cat1
    2 | cat2
    3 | cat3
    4 | cat4
    5 | cat5
    6 | cat6

运行此sql代码时

SELECT  c.* ,f.Amount, f.Item FROM item_table f
   LEFT JOIN (
   SELECT * FROM category c LIMIT '.$offset.', 3
   )
 AS c ON f.cat_id = c.id ORDER BY c.id desc

它像这样归还我

  a   |  b   | items
 -----+-------------
    3 | cat3 | item,item...
    2 | cat2 | item,item...
    1 | cat1 | item,item...

但我希望它能让我回头

  a   |  b   | items
 -----+-------------
    6 | cat6 | item,item...
    4 | cat4 | item,item...
    5 | cat5 | item,item...

我怎么做?

1 个答案:

答案 0 :(得分:0)

在子查询中添加order by子句。

SELECT  c.* ,f.Amount, f.Item FROM item_table f
   LEFT JOIN (
   SELECT * FROM category c 
   ORDER BY c.id desc
   LIMIT '.$offset.', 3
   )
 AS c ON f.cat_id = c.id
 ORDER BY c.id desc