1

I created the following query in order to produce a two-level navigation, Level one - categories Level two - subcategories

Ex. category - Year<br />
subcategories - 2013, 2012, 2011, 2010, 2009

It looks like this:

$query = "
   SELECT categories.Category, categories.idCat, subcategories.subCategory, subcategories.idSub       
   FROM 
      categories 
   JOIN 
      cat_sub ON categories.idCat = cat_sub.idCat
   JOIN 
      subcategories ON subcategories.idSub = cat_sub.idSub
   ORDER BY 
      categories.idCat DESC,subcategories.idSub DESC";

The tables looks like this:

categories(idCat, Category)

subcategories(idSub,  subCategory)

cat_sub(idCat,  idSub)

I want to LIMIT the amount of subcategories to three, while keeping categories unlimited. Any help would be much appreciated!

Ex. ONLY DISPLAY
category - Year
subcategories - 2013, 2012, 2011

Hope I made things a bit more clear.

Thanks, Aleks

4

2 回答 2

1

That's tricky, but we can work it out with some user variables:

$query = "
SELECT
    c.Category,
    c.idCat,
    s.subCategory,
    s.idSub
FROM
    categories c 
        JOIN cat_sub cs ON c.idCat = cs.idCat
        JOIN (
            SELECT 
                IF(@C != c.Category, @ROWNUM := 1, @ROWNUM := @ROWNUM +1) AS RN,
                @C := c.Category,
                c.idCat,
                s.idSub,
                s.subCategory
            FROM categories c 
                JOIN cat_sub cs ON c.idCat = cs.idCat
                JOIN subcategories s ON s.idSub = cs.idSub
                CROSS JOIN (SELECT @C := '') t2
            ORDER BY c.idCat DESC, s.idSub DESC
        ) s ON s.idSub = cs.idSub AND cs.idCat = s.idCat
        JOIN (
            SELECT idCat, MAX(rn) AS mx
            FROM (
                SELECT 
                    IF(@C != c.Category, @ROWNUM := 1, @ROWNUM := @ROWNUM +1) AS rn,
                    @C := c.Category,
                    c.idCat
                FROM categories c 
                    JOIN cat_sub cs ON c.idCat = cs.idCat
                    JOIN subcategories s ON s.idSub = cs.idSub
                    CROSS JOIN (SELECT @C := '') t2
                ORDER BY c.idCat DESC, s.idSub DESC
            ) t
            GROUP BY idCat
        ) maxcat ON maxcat.idCat = c.idCat AND s.rn BETWEEN maxcat.mx-2 AND maxcat.mx
ORDER BY c.idCat DESC, s.idSub DESC";

It would be more elegant with a temporary table or a view before that, but I chose to stick to regular sql.

fiddle: http://sqlfiddle.com/#!2/2820b/17

于 2013-11-14T05:10:31.103 回答
0

Use a derived table that to limit the joined subcategories. Something like this:

SELECT categories.Category, categories.idCat, T.subCategory, T.idSub       
   FROM 
      categories 
   JOIN 
      cat_sub ON categories.idCat = cat_sub.idCat
   JOIN 
      (SELECT * FROM subcategories s ORDER BY year DESC LIMIT 3) as T ON T.idSub = cat_sub.idSub 
   ORDER BY 
      categories.idCat DESC,T.idSub DESC";

Note: This is untested, since you have not provided your table structures.

于 2013-11-14T06:19:33.537 回答