0
(
SELECT * 
FROM (

SELECT d
FROM myTable
WHERE id =  "4h"
AND d <  "2011-12-08 12:00:00"
ORDER BY d DESC 
LIMIT 10
)tmp
ORDER BY d ASC
)
UNION (

SELECT d
FROM myTable
WHERE id =  "4h"
AND d >=  "2011-12-08 12:00:00"
ORDER BY d ASC 
LIMIT 10
)

我试图通过使用两个SELECT语句和一个UNION. 第一个SELECT用于ORDER BY DESC获取前面的 10 个,然后我尝试在一秒钟内将其封装以ORDER BY ASC按顺序获取所有结果,ASC但由于某种原因它不起作用。

这是我目前得到的结果:

d
2011-12-08 08:00:00
2011-12-08 04:00:00
2011-12-08 00:00:00
2011-12-07 20:00:00
2011-12-07 16:00:00
2011-12-07 12:00:00
2011-12-07 08:00:00
2011-12-07 04:00:00
2011-12-07 00:00:00
2011-12-06 20:00:00 <- These top 10 results should ASC!
2011-12-08 12:00:00
2011-12-08 16:00:00
2011-12-08 20:00:00
2011-12-09 00:00:00
2011-12-09 04:00:00
2011-12-09 08:00:00
2011-12-09 12:00:00
2011-12-09 16:00:00
2011-12-09 20:00:00
2011-12-11 20:00:00

这就是我想要的:

d
2011-12-06 20:00:00
2011-12-07 00:00:00
2011-12-07 04:00:00
2011-12-07 08:00:00
2011-12-07 12:00:00
2011-12-07 16:00:00
2011-12-07 20:00:00
2011-12-08 00:00:00
2011-12-08 04:00:00
2011-12-08 08:00:00
2011-12-08 12:00:00
2011-12-08 16:00:00
2011-12-08 20:00:00
2011-12-09 00:00:00
2011-12-09 04:00:00
2011-12-09 08:00:00
2011-12-09 12:00:00
2011-12-09 16:00:00
2011-12-09 20:00:00
2011-12-11 20:00:00
4

1 回答 1

2
(
  SELECT   d
  FROM     myTable
  WHERE    id = '4h' AND d <  '2011-12-08 12:00:00'
  ORDER BY d DESC
  LIMIT    10
) UNION ALL (  
  SELECT   d
  FROM     myTable
  WHERE    id = '4h' AND d >= '2011-12-08 12:00:00'
  ORDER BY d ASC
  LIMIT    10
)
ORDER BY d ASC
于 2012-09-04T21:20:55.240 回答