开发者

Mysql: Solution for complex query. Multiple limits

var statements=[];
for(i=1;i<6;i++)
{
    statements.push(
        '(SELECT a.category, a.id,a.country,a.region,a.rajons,a.town AS town_id,img.t_0, img.main, mt.town, mp.pagasts, mp.pagasts_id, c.link_ AS link'
        +' FROM ads a'
                +' INNER JOIN categories c ON a.category = c.cat_id'
                +' RIGHT JOIN (SELECT t_0,main,ad_id FROM images ORDER BY main DESC) AS img ON a.id = img.ad_id'
                +' LEFT JOIN map_regions mr ON a.region = mr.region_id'
                +' LEFT JOIN map_towns mt ON a.town = mt.town_id'
                +' LEFT JOIN map_pagasts mp ON a.pagasts = mp.pagasts_id'
            +' WHERE a.region='+i
                +' AND a.department !=9'
                +' AND (a.town > 0 OR a.pagasts > 0)'
                +' AND a.charged=1'
            +' GROUP BY a.id'
            +' ORDER BY a.id DESC LIMIT 5)'
    );
}

statements.join(' UNION ALL ')

This is the main moment in the query:

WHERE a.region='+i

And

LIMIT 5

Is it possible to开发者_高级运维 do it some other way, without UNION ALL?

Thanks ;)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜