views:

38

answers:

1

Here is current SQL: http://www.copypastecode.com/22205/

It takes 70 new rows from table ads.

But it doesn't take proper amount(for example 5) of rows for each mr.region_id from joined table map_regions.

For example if I will add 50 ads in one region, it will take all 50 of them and leave 20 slots for the rest regions.

Please help me to upgrade current SQL, so it would take 5 rows from table ads for each mr.region_id in map_regions table.

A: 

Try

SELECT a.id,a.country,a.region,a.rajons,a.town AS town_id,a.pagasts,i.t0_1, mt.town, mp.pagasts, c.link
FROM ads a 
INNER JOIN categories c ON a.category = c.cat_id
RIGHT JOIN images i ON a.id = i.ad_id
LEFT JOIN (select * from map_regions where region_id = a.region LIMIT 5) as mr
LEFT JOIN map_towns mt ON a.town = mt.town_id
LEFT JOIN map_pagasts mp ON a.pagasts = mp.pagasts_id
WHERE a.expire >= ' . $current->get() . '
ORDER by a.id DESC
LIMIT 70
Sam Dark
SQLSTATE[42000]: Syntax error or access violation: 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 'WHERE a.expire >= 1266169150 ORDER by a.id DESC LIMIT 70' at line 8
Beck
MySQL version: 5.1.41
Beck
I'm using PDO connection to database. Maybe this is the reason of syntax error?
Beck
So there is no way to do it via one query?
Beck
There should be a way. I'll give it another try later today.
Sam Dark
Tried another one. Have not found and satisfying solution.
Sam Dark
Well, maybe someone will answer later.Anyway thanks.Question is stil open.
Beck