有这样的sql:
(select `ads`.*
from `ads`
where `status` = ?
and `lat` between ? and ?
and `lng` between ? and ?
and NOW() between ads.featured_start and ads.featured_end
order by `ads`.`id` desc)
union
(select `ads`.*
from `ads`
where `status` = ?
and `lat` between ? and ?
and `lng` between ? and ?
and NOW() not between ads.featured_start and ads.featured_end
order by `ads`.`id` desc)
limit 8 offset 0
但是,得到id = 1,2,3,4,5的结果......(这是必要的5,4,3,2,1) 为什么?请帮帮我)
答案 0 :(得分:0)
尝试类似:
(select `ads`.*
from `ads`
where `status` = ?
and `lat` between ? and ?
and `lng` between ? and ?
and NOW() between ads.featured_start and ads.featured_end
union
select `ads`.*
from `ads`
where `status` = ?
and `lat` between ? and ?
and `lng` between ? and ?
and NOW() not between ads.featured_start and ads.featured_end)
ORDER BY id DESC
limit 8 offset 0
答案 1 :(得分:0)
代码模式是
( SELECT ... ORDER BY ... LIMIT .. )
UNION ALL? DISTINCT? -- Which do you need?
( SELECT ... ORDER BY ... LIMIT .. )
ORDER BY ... LIMIT .. OFFSET ..;
是的,这是ORDER BY ...
的3份副本。
在外面,你有LIMIT n OFFSET m,但内部使用LIMIT m + n。
3份副本的原因是最小化tmp表大小 - 至少有3个tmp表。