使用 ORDER BY 子句查询并在结果末尾分别发送某些行
Query with ORDER BY clause and sending certain rows at the end of the results separately
考虑到我想查询单个 table 而这个 table 有这个 structure 和 data
price | weight
100 0
200 10
500 0
300 10
我想应用 ORDER BY price DESC 但 我还想要 weight = 10 的结果在结果的末尾(这些结果也应用 ORDER BY price DESC)。
Results Query A WHERE weight <> 10
----------------
price | weight
100 0
500 0
Results Query B WHERE weight = 10
----------------
price | weight
200 10
300 10
我想过使用 UNION 但我无法实现我想要的:
EXPECTED RESULT <= order by price DESC
----------------
price | weight
500 0
100 0
300 10 <=
200 10 <= but maintaining rows with weight 10 at the end of the results
你会怎么做?提前致谢
如果您先按布尔表达式 weight = 10
排序,所有此类行将放在结果集的底部:
ORDER BY weight = 10,
price DESC
因为 weight = 10
被评估为 true
的 1
和 false
的 0
。
考虑到我想查询单个 table 而这个 table 有这个 structure 和 data
price | weight
100 0
200 10
500 0
300 10
我想应用 ORDER BY price DESC 但 我还想要 weight = 10 的结果在结果的末尾(这些结果也应用 ORDER BY price DESC)。
Results Query A WHERE weight <> 10
----------------
price | weight
100 0
500 0
Results Query B WHERE weight = 10
----------------
price | weight
200 10
300 10
我想过使用 UNION 但我无法实现我想要的:
EXPECTED RESULT <= order by price DESC
----------------
price | weight
500 0
100 0
300 10 <=
200 10 <= but maintaining rows with weight 10 at the end of the results
你会怎么做?提前致谢
如果您先按布尔表达式 weight = 10
排序,所有此类行将放在结果集的底部:
ORDER BY weight = 10,
price DESC
因为 weight = 10
被评估为 true
的 1
和 false
的 0
。