views:

400

answers:

6

I have a database of items. Each item is categorized with a category ID from a category table. I am trying to create a page that lists every category, and underneath each category I want to show the 4 newest items in that category.

For Example:

Pet Supplies
img1 img2 img3 img4

Pet Food
img1 img2 img3 img4

I know that I could easily solve this problem by querying the database for each category like so:

SELECT id FROM category

Then iterating over that data and querying the database for each category to grab the newest items:

SELECT image FROM item where category_id = :category_id 
ORDER BY date_listed DESC LIMIT 4

What I'm trying to figure out is if I can just use 1 query and grab all of that data. I have 33 categories so I thought perhaps it would help reduce the number of calls to the database.

Anyone know if this is possible? Or if 33 calls isn't that big a deal and I should just do it the easy way.

Thanks!

Answer Notes
I have chosen mjv's answer simple because he spent a great deal amount of time catering it for my specific application. However for anyone else out there I would highly recommend Bill's solution.

A: 

not very pretty but:

SELECT image 
FROM item 
WHERE date_listed IN (SELECT date_listed 
                      FROM item 
                      ORDER BY date_listed DESC LIMIT 4)
tster
not working mysql doesn't support LIMIT in subquery
RageZ
This would need to be called for each category, right? Is there a way to group it all into 1 query?
justinl
oops, didn't know you couldn't do LIMIT in a subquery
tster
everybody learns!
justinl
Another problem with this: multiple images might have the same date_listed, and you could end up with incorrect data
Steve McLeod
You can do a limit in a subquery, it just has to be a limit of 1.
Jage
A: 

ok after a googling the quick answer would it's not possible at least on mysql

this this thread for reference

maybe you should cache the result of that query if you are afraid to make fall down the server and you want the code to perform more well

RageZ
A: 

Depending on how constant your categories are, the following is the simplest route

SELECT C.CategoryName, R.Image, R.date_listed
FROM
(
    SELECT CategoryId, Image, date_listed
    FROM 
    (
      SELECT CategoryId, Image, date_listed
      FROM item
      WHERE Category = 'Pet Supplies'
      ORDER BY date_listed DESC LIMIT 4
    ) T

    UNION ALL

    SELECT CategoryId, Image, date_listed
    FROM
    (        
      SELECT CategoryId, Image, date_listed
      FROM item
      WHERE Category = 'Pet Food'
      ORDER BY date_listed DESC LIMIT 4
    ) T
) RecentItemImages R
INNER JOIN Categories C ON C.CategoryId = R.CategoryId
ORDER BY C.CategoryName, R.Image, R.date_listed
David Andres
Thanks David. So is this way of combining all the queries into 1 big query more efficient than doing 33 separate queries (1 for each category)?
justinl
Yes, it can be, if only for the fact that you're probably doing your 33 separate queries as separate requests from the database. Some of that time is spent simply shuttling data back and forth to/from the database server. I've also modified the UNION to a UNION ALL, which does not check for and remove duplicates. You probably wouldn't have any in any case.
David Andres
Thanks. You are correct that I won't have any duplicates because all the items have a PK.Also it seems like I could just build a query by querying all the category ID's and then building a query by iterating over those results and combining them into a string and using that string as the new query.
justinl
If that's what you want to do. I say why bother, particularly if you're telling me that category changes don't happen often. If that's the case, copy and paste. When categories change you can come back to this query and make the appropriate modifications. It won't be automatic, but it will work.
David Andres
I just realized I don't understand in your query how to JOIN the categories. E.g. How do those SELECT statements know what Category is? Because the category ID and name is in another table.
justinl
I'll edit the post accordingly...
David Andres
Your OP has the category id and image in the item table, so that's what I worked off of.
David Andres
Thanks David. I don't understand this line: WHERE C.CategoryId = :category_id. What does :category_id refer to? I used that in my example as a placeholder for the category_id. But I thought this query would span across all categories, and thus, wouldn't need a WHERE statement
justinl
I'm also receiving the error #1221 - Incorrect usage of UNION and ORDER BY
justinl
Sorry, ORDER BY statements should only appear after UNION/UNION ALL (my mistake). Due to this, I had to structure the query I little differently. I had the WHERE :category_id line to show how you could filter this if you wanted.
David Andres
+3  A: 

This solution is an adaptation from another SO solution, thank you RageZ for locating this related/similar question.

NOTE

This solution seems satisfactory for Justin's use case. Depending on your use case you may want to check Bill Karwin or David Andres' solutions in this posting. Bill's solution has my vote! See why, as I put both queries next to one another ;-)

The benefit of my solution is that it returns one record per category_id (the info from the item table is "rolled-up"). The main drawback of my solution is its lack of readability and its growing complexity as the number of desired rows grows (say to have 6 rows per category rather than 6). Also it may be slightly slower as the number of rows in the item table grows. (Regardless, all solutions will perform better with a smaller number of eligible rows in the item table, and it is therefore advisable to either periodically delete or move older items and/or to introduce a flag to help SQL filter out rows early)

First try (didn't work!!!)...

The problem with this approach was that the subquery would [rightfully but bad for us] produce very many rows, based on the cartesian products defined by the self joins...

SELECT id, CategoryName(?), tblFourImages.*
FROM category
JOIN (
    SELECT i1.category_id, i1.image as Image1, i2.image AS Image2, i3.image AS Image3, i4.image AS Image4
    FROM item AS i1
    LEFT JOIN item AS i2 ON i1.category_id = i2.category_id AND i1.date_listed > i2.date_listed
    LEFT JOIN item AS i3 ON i2.category_id = i3.category_id AND i2.date_listed > i3.date_listed
    LEFT JOIN item AS i4 ON i3.category_id = i4.category_id AND i3.date_listed > i4.date_listed
) AS tblFourImages ON tblFourImages.category_id = category.id
--WHERE  here_some_addtional l criteria if needed
ORDER BY id ASC;

Second try. (works ok!)

A WHERE clause in added for the subquery, forcing the date listed to be the latest, second latest, thrird lateest etc. for i1, i2, i3 etc. respectively (and also allowing for the null cases when there are fewer than 4 items for a given category id). Also added was unrelated filter clauses to prevent showing entries that are "sold" or entries that do not have an image (added requirements)

This logic makes the assumption that there are no duplicate date listed values (for a given category_id). Such cases would otherwise create duplicate rows. Effectively this use of the date listed is that of a monotonically incremented primary key as defined/required in Bill's solution.

SELECT id, CategoryName, tblFourImages.*
FROM category
JOIN (
    SELECT i1.category_id, i1.image as Image1, i2.image AS Image2, i3.image AS Image3, i4.image AS Image4, i4.date_listed
    FROM item AS i1
    LEFT JOIN item AS i2 ON i1.category_id = i2.category_id AND i1.date_listed > i2.date_listed AND i2.sold = FALSE AND i2.image IS NOT NULL
          AND i1.sold = FALSE AND i1.image IS NOT NULL
    LEFT JOIN item AS i3 ON i2.category_id = i3.category_id AND i2.date_listed > i3.date_listed AND i3.sold = FALSE AND i3.image IS NOT NULL
    LEFT JOIN item AS i4 ON i3.category_id = i4.category_id AND i3.date_listed > i4.date_listed AND i4.sold = FALSE AND i4.image IS NOT NULL
    WHERE NOT EXISTS (SELECT * FROM item WHERE category_id = i1.category_id AND date_listed > i1.date_listed)
      AND (i2.image IS NULL OR (NOT EXISTS (SELECT * FROM item WHERE category_id = i1.category_id AND date_listed > i2.date_listed AND date_listed <> i1.date_listed)))
      AND (i3.image IS NULL OR (NOT EXISTS (SELECT * FROM item WHERE category_id = i1.category_id AND date_listed > i3.date_listed AND date_listed <> i1.date_listed AND date_listed <> i2.date_listed)))
      AND (i4.image IS NULL OR (NOT EXISTS (SELECT * FROM item WHERE category_id = i1.category_id AND date_listed > i4.date_listed AND date_listed <> i1.date_listed AND date_listed <> i2.date_listed AND date_listed <> i3.date_listed)))
) AS tblFourImages ON tblFourImages.category_id = category.id
--WHERE  --
ORDER BY id ASC;

Now... compare the following where I introduce an item_id key and use Bill's solution to provide the list of these to the "outside" query. You can see why Bill's approach is better...

SELECT id, CategoryName, image, date_listed, item_id
FROM item I
LEFT OUTER JOIN category C ON C.id = I.category_id
WHERE I.item_id IN 
(
SELECT i1.item_id
FROM item i1
LEFT OUTER JOIN item i2
  ON (i1.category_id = i2.category_id AND i1.item_id < i2.item_id
      AND i1.sold = 'N' AND i2.sold = 'N'
      AND i1.image <> '' AND i2.image <> ''
      )
GROUP BY i1.item_id
HAVING COUNT(*) < 4
)
ORDER BY category_id, item_id DESC
mjv
Now I get: #1054 - Unknown column 'date_listed' in 'order clause'If I remove the date_listed from the ORDER clause it does work, but it seems to not iterate over the different categories, but instead just lists out the same category over and over again
justinl
Okay i got date_listed figured out (I just added it to the JOIN's subquery like we did with the category_id). But each row of the returned result is showing the same categoryName, ID, and image path
justinl
haha it's so close. but the rows that are returned are all from the same category (even though I have half a dozen items in different categories).
justinl
Actually, I feel bad, I got you on this track, but there's a flaw with the design. Basically the subquery produces [rightfully but bad for us] a whole slew of rows from the cartesian product expressed by the self joins. Another side issue, which we can address once this issue is solved, is that as written now, there could not be any two record in image table with same date _and_ same category_id...
mjv
No worries about my time. It's a bit like a challenge, plus a nice ego check, when "simple" stuff like that ends up blowing up in my face... I'll give it another 30 minutes...
mjv
Ok, Justin. I got it. a few more minutes... One question: can you confirm that for a given category_id, the date_listed for all items will differ (by at least a second) ?
mjv
I can't confirm it exactly because the items are user generated. but the date_listed is logged by seconds, so it won't happen too often. And if it does, I don't mind losing that item and it just shows the next one
justinl
I should also note that there are 2 other parameters that I will be checking for on each item. First is that each item has a bool sold column. I don't want to show an item if it's sold. Also, I don't want to have items returned that have no images. The WHERE clause would be something like WHERE sold = 0 AND image <> ''
justinl
Ok, Justin, I added the sold and image constraint. I added these at the level of each join, for symmetry/consistency, except for i1 table which is in the where clause. ATTENTION, being on mySQL, the NOT NULL test may need to be a bit different (but easy to change)
mjv
@justin, with your ok, I'll delete some of these comments (mine), not so much to erase evidence of this long trek, but to clean things up. I think we should somehow qualify this solution as workable, with friendly output etc., but otherwise honor Bill's simpler solution.
mjv
Btw it sounds like your not using mySQL to test this on your end. On my end I get the following error: #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'NOT EXISTS (SELECT * FROM item WHERE category_id = i1.category_id AND date' at line 10
justinl
OK, the not showing any item if an item had "sold" is fixed. It is updated in the response code. Basically the i1.sold=true and i1.image is not null clause was moved from the where to the join condition with i2.
mjv
@justin Thank you for your kind acceptance. Rewarding effort not brains, in this case ;-) I edited my posting as so to nudge future readers away from my not so elegant solution. Unless one wishes to use the roll-up logic and is sure to ever need only the top 3 or 4 items for each group, there is not point to self impose such abuse. 't was fun none the less, I hope I didn't waste too much of _your_ time.
mjv
@justin Pls do kill some of your comments here, for clean up / clarity purposes.
mjv
+8  A: 

This is the greatest-n-per-group problem, and it's a very common SQL question.

Here's how I solve it with outer joins:

SELECT i1.*
FROM item i1
LEFT OUTER JOIN item i2
  ON (i1.category_id = i2.category_id AND i1.item_id < i2.item_id)
GROUP BY i1.item_id
HAVING COUNT(*) < 4
ORDER BY category_id, date_listed;

I'm assuming the primary key of the item table is item_id, and that it's a monotonically increasing pseudokey. That is, a greater value in item_id corresponds to a newer row in item.

Here's how it works: for each item, there are some number of other items that are newer. For example, there are three items newer than the fourth newest item. There are zero items newer than the very newest item. So we want to compare each item (i1) to the set of items (i2) that are newer and have the same category as i1. If the number of those newer items is less than four, i1 is one of those we include. Otherwise, don't include it.

The beauty of this solution is that it works no matter how many categories you have, and continues working if you change the categories. It also works even if the number of items in some categories is fewer than four.

Bill Karwin
Hi Bill. Thanks that works great! Something I didn't mention in the question, is that I only want to return results that have an image, if the image is blank, then I don't want it to be returned. How can I put a WHERE clause in there so that it doesn't return an image if the item.image = ''
justinl
Ah I just figured it out. I put a "WHERE image <> '' " above the GROUP BY line
justinl
FYI: If you want to constrain against other table columns you have to do so in the ON brackets, and using a WHERE just above the GROUP BYeg: ON (i2.active = TRUE)WHERE i1.active = TRUE
justinl
If you want this to scale down to top 1 item, then the clause on the item ids needs to be `i1.item_id <= i2.item_id`.
Porges
@Porges: I don't think that's right.
Bill Karwin
A: 

the code below shows a way to do it in a loop it definetely needs a lot of editing, but i hope it helps.

    declare @RowId int

declare @CategoryId int declare @CategoryName varchar(MAX)

create table PART (RowId int, CategoryId int, CategoryName varchar) create table NEWESTFOUR(RowId int, CategoryId int, CategoryName varchar, Image image) select RowId = ROW_NUMBER(),CategoryId,CategoryName into PART from [Category Table]

    set @PartId = 0

set @CategoryId = 0 while @Part_Id <= --count begin set @PartId = @PartId + 1 SELECT @CategoryId = category_id, @CategoryName = category_name from PART where PartId = @Part_Id SELECT RowId = @PartId, image,CategoryId = @category_id, CategoryName = @category_name FROM item into NEWESTFOUR where category_id = :category_id ORDER BY date_listed DESC LIMIT 4

end select * from NEWESTFOUR drop table NEWESTFOUR drop table PART

Ali YILDIRIM