SQL 首先选择某个值

发布于 2024-11-02 06:12:20 字数 287 浏览 2 评论 0原文

SELECT     AssetValue
FROM         Assets
WHERE     (AssetType = 'Country')

非常简单的Select语句,但它输出

Afghanistan
Albania
Algeria
....

How do I make United States and Canada出现在这上面?这可以通过 SQL 实现吗?或者我需要在 ASP 代码中执行某些操作吗?我的PK是INT身份。

SELECT     AssetValue
FROM         Assets
WHERE     (AssetType = 'Country')

Very simple Select Statement, but it outputs

Afghanistan
Albania
Algeria
....

How do I make United States and Canada appear on the top of this? Is this possible with SQL or do I need to do something in my ASP code? My PK is a INT Identity.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

满意归宿 2024-11-09 06:12:20
SELECT     AssetValue
FROM         Assets
WHERE     (AssetType = 'Country')
ORDER BY CASE AssetValue 
          WHEN 'US' THEN 1 
          WHEN 'Canada' THEN 2 ELSE 3 END, AssetValue 
SELECT     AssetValue
FROM         Assets
WHERE     (AssetType = 'Country')
ORDER BY CASE AssetValue 
          WHEN 'US' THEN 1 
          WHEN 'Canada' THEN 2 ELSE 3 END, AssetValue 
年少掌心 2024-11-09 06:12:20

如果您不希望硬编码,最通用的解决方案是使用额外的字段来指示优惠项目。您可以将其称为OrderPreference。 OrderPreference 越高,第一个项目就会出现。其他每个项目的 OrderPreference 都为零。查询:

SELECT     AssetValue
FROM         Assets
WHERE     (AssetType = 'Country')
ORDER BY OrderPreference desc, AssetValue

从表面上看,资产是一个用于其他事物(不仅仅是国家)的表,因此您可以使用此方法来解决其他资产类型(如果出现)的相同问题。

If you do not wish to hard code, the most generic solution is having an extra field to indicate the preferential items. You would call it OrderPreference. The higher OrderPreference, the first the item would appear. Every item else would have OrderPreference equals ZERO. The query:

SELECT     AssetValue
FROM         Assets
WHERE     (AssetType = 'Country')
ORDER BY OrderPreference desc, AssetValue

By the looks of it, Assets is a table you use for other things (not only countries), so you could use this approach to solve the same problem for others asset types, if they come up.

め七分饶幸 2024-11-09 06:12:20

给他们一个组ID,所以组1是美国、英国等,组2是世界其他国家,那么你可以做“优先”国家。

Give them a a group ID, so group 1 is US, UK etc, group2 is the rest of the world, then you can do "priority" countries.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文