I have been trying to figure this out for hours and seems that I'm stuck.
When I want to do in the query below is that I am passing some location IDs to IN
operator and I don't want MySQL to return ALL the results (table is too large) but only 5 items of every location ID (if items exist).
I have temporarily put a LIMIT 100
until I get it done with your help.
Here's the query:
$sql = "SELECT SQL_CACHE deals.deal_ID, deals.slug, deals.url, deals.previous_price, deals.title, deals.image, deals.price, deals.end, deals.purchases, deals.date_added,
deals_locations.location_ID, deals.hits
FROM deals
INNER JOIN deals_locations
WHERE deals.status = 'active'
AND deals_locations.deal_ID = deals.deal_ID
AND deals_locations.location_ID IN (".implode(', ', $location_IDs).")
GROUP BY deals.deal_ID
LIMIT 100;";
Thank you very much for your time.
EDIT: Now my query output looks like this:
SELECT SQL_CACHE deals.deal_ID
,deals.slug
,deals.url
,deals.previous_price
,deals.title
,deals.image
,deals.price
,deals.end
,deals.purchases
,deals.date_added
,deals_locations.location_ID
,deals.hits
FROM deals
INNER JOIN deals_locations
WHERE deals.status = 'active'
AND (
SELECT COUNT(*) FROM deals
WHERE deals.deal_ID = deals_locations.deal_ID
) <= 5
AND deals_locations.deal_ID = deals.deal_ID
AND deals_locations.location_ID IN (1, 2, 3, 5, 7, 12, 13, 26, 27, 28, 29, 30, 31, 34, 35, 36, 38, 39, 40, 41, 42, 43, 44, 46, 49, 52, 53, 54, 55, 56, 60, 62, 64, 65, 66, 67, 68, 69, 70, 72, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 89, 90, 97, 107, 10, 21, 32, 33, 37, 51, 4, 6, 8, 9, 11, 14, 15, 16, 17, 18, 19, 20, 22, 23, 24, 25, 45, 47, 48, 50, 57, 58, 59, 61, 63, 71, 73, 84, 85, 86, 87, 88, 91, 92, 93, 94, 95, 96, 98, 99, 100, 101, 102, 103, 104, 105, 106, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160)
GROUP BY deals.deal_ID;
But still shows more than 5 records for the same location.
EDIT 2: I have tried the UNION ALL
approach and seems to be working. Now I get a total result of 30 rows but I am concerned about the size of the query.
Here's what my query looks like now: http://pastebin.com/r6vyG594
The explain
is huge so I can't post it online. What should I do to make the query efficient and smaller in size?
It takes about 0.2158 sec to execute.