ORDER BY select 语句使用 UNION &清楚的

ORDER BY select statement using UNION amp; DISTINCT(ORDER BY select 语句使用 UNION amp;清楚的)
本文介绍了ORDER BY select 语句使用 UNION &清楚的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在使用以下查询来填充值的下拉列表.

I'm using the following query to populate a dropdown list of values.

select 'Select a City' as City, 'All' as Value
UNION ALL
select distinct City, City as Value from BND_Listing

我想对结果进行 A-Z 排序.我尝试了以下方法:

I'd like to sort A-Z the results. I've tried the following:

select 'Select a City' as City, 'All' as Value
UNION ALL
select distinct City, City as Value from BND_Listing
ORDER BY City ASC

但是我收到一个错误:

关键字联合"附近的语法不正确.

Incorrect syntax near the keyword 'Union'.

此外,此查询正在提取Blank or NULL"值并在下拉列表顶部显示一个空格.如果可能的话,我想隐藏它.不显示任何空值?

Additionally this query is pulling "Blank or NULL" values and displaying a blank space at the top of the drop-down. I'd like to hide that if possible. Not display any null value?

推荐答案

感谢大家的回复,它们让我对在哪里查找问题有了很多见解.添加以下内容的原始查询获得了正确的结果.

Thank you everyone for the responses it gave me a lot of insight on where to look for my problem. The original query with the addition of the below achieved the proper result.

工作查询:

select 'Select a City' as City, 'All' as Value
UNION ALL
select distinct City, City as Value from BND_Listing
where isnull(City,'') <> ''
Order by City ASC

选择城市"始终位于下拉列表的顶部.感谢@scsimon 在我的另一篇文章中对此的贡献.

with 'Select a City' always at the top of the dropdown. Credit to @scsimon on my other post for this.

with cte as(
select 'Select a City' as City, 'All' as Value
UNION ALL
select distinct City, City as Value from BND_Listing
where isnull(City,'') <> '')


select * from cte  Order by case when City = 'Select a City' then 1 else 2     end, City ASC

这篇关于ORDER BY select 语句使用 UNION &amp;清楚的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Query with t(n) and multiple cross joins(使用 t(n) 和多个交叉连接进行查询)
Unpacking a binary string with TSQL(使用 TSQL 解包二进制字符串)
Max rows in SQL table where PK is INT 32 when seed starts at max negative value?(当种子以最大负值开始时,SQL 表中的最大行数其中 PK 为 INT 32?)
Inner Join and Group By in SQL with out an aggregate function.(SQL 中的内部连接和分组依据,没有聚合函数.)
Add a default constraint to an existing field with values(向具有值的现有字段添加默认约束)
SQL remove from running total(SQL 从运行总数中删除)