SELECT id,region FROM ads
RIGHT JOIN (SELECT * FROM ads a2 WHERE a2.region=ads.region LIMIT 4) AS regions
ON regions.id=ads.id
说“未知列ads.region”。如何访问主表列区域?
我需要为每个区域获取4行
答案 0 :(得分:3)
使用:
SELECT a.id,
a.region
FROM ads a
RIGHT JOIN (SELECT t.*,
CASE
WHEN @region = t.region THEN @rownum := @rownum + 1
ELSE @rownum := 1
END as rank,
@region := t.region
FROM ADS t
JOIN (SELECT @rownum := 0, @region := 1) r
ORDER BY t.region) b ON b.id = a.id
AND b.region = a.region
AND b.rank <= 4