Home Database Mysql Tutorial 利用 Case ?述做 SQL 的??排列

利用 Case ?述做 SQL 的??排列

Jun 07, 2016 pm 03:04 PM
case server sql w use arrangement

sql server (WINDOWS平台上强大的数据库平台) 在?取?料?是?用??的方式, 所以使用者?用 Order 子句?做?料 排列 , 在?篇文章?中咱叨要??一步介?按怎使用 Case ?做??的 排列 Order By 子句 咱先?看?一般使用 Select 的?果, 在?篇文章的例咱?使用 sql server (WI


  sql server(WINDOWS平台上强大的数据库平台) 在?取?料?是?用??的方式, 所以使用者?用 Order 子句?做?料排列, 在?篇文章?中咱叨要??一步介?按怎使用 Case ?做??的排列

Order By 子句

咱先?看?一般使用 Select 的?果, 在?篇文章的例咱?使用 sql server(WINDOWS平台上强大的数据库平台) 的北??料?, ?打? Query Analyzer, ?行

Use Northwind
Go
Select CompanyName, ContactName, ContactTitle
From Customers

?行的?果可能是按照?料?入的???排列, 哪是你要照自己的???排, 你叨需要用到 Order By 子句, 譬如?前一?例, 你哪是要照公司名?排叨要按呢做

Select CompanyName, ContactName, ContactTitle
From Customers
Order By CompanyName

Case ?述

Case ?述?用?控制程式的流向, 所以咱叨要利用 Case ?述?做 T-SQL 的??控制, ?看? Case 的?法

Case
When Then
[...n]
[Else ]

光看?法不?, ?在用一?例???一下, 同款地咱?是用北??料?, 假?咱?要??? (Employees) ?料表中的英文?呼改做台?式, 可以按呢做

select FirstName, TitleOfCourtesy, CourtesyInTC =
 Case TitleOfCourtesy
    When 'Ms.' Then '水姑娘'
    When 'Dr.' Then '博士博'
    When 'Mr.' Then '先仔'
    When 'Mrs.' Then '女士'
    Else '先生/小姐'
 End
From employees

??排列

了解了基本?法後咱?就可以?始??按怎利用 Order By 加上 Case ?述?做??的?料排列

Declare @OrderKey TinyInt
Set @OrderKey = 2
 
Select CompanyName, ContactName, ContactTitle
From Customers
Order By Case
 When @OrderKey = 1 Then CompanyName
 When @OrderKey = 2 Then ContactName
 Else ContactTitle
End

不???像?不太?用, 至少在 ASP 中, 所以接下?咱?做一???程序

Create Procedure sp_SelectCustomersDynamicOrder
@OrderKey = Null
As
Select CompanyName, ContactName, ContactTitle
From Customers
Order By Case
 When @OrderKey = 1 Then CompanyName
 When @OrderKey = 2 Then ContactName
 Else ContactTitle
End

?在你就可以在直接呼叫?? Stored Procedure

exec sp_SelectCustomersDynamicOrder
exec sp_SelectCustomersDynamicOrder 1
exec sp_SelectCustomersDynamicOrder 2

按呢是不是友善多了

希望?篇文章?你有?助! 
    

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Do I need to use flexbox in the center of the Bootstrap picture? Do I need to use flexbox in the center of the Bootstrap picture? Apr 07, 2025 am 09:06 AM

There are many ways to center Bootstrap pictures, and you don’t have to use Flexbox. If you only need to center horizontally, the text-center class is enough; if you need to center vertically or multiple elements, Flexbox or Grid is more suitable. Flexbox is less compatible and may increase complexity, while Grid is more powerful and has a higher learning cost. When choosing a method, you should weigh the pros and cons and choose the most suitable method according to your needs and preferences.

How to calculate c-subscript 3 subscript 5 c-subscript 3 subscript 5 algorithm tutorial How to calculate c-subscript 3 subscript 5 c-subscript 3 subscript 5 algorithm tutorial Apr 03, 2025 pm 10:33 PM

The calculation of C35 is essentially combinatorial mathematics, representing the number of combinations selected from 3 of 5 elements. The calculation formula is C53 = 5! / (3! * 2!), which can be directly calculated by loops to improve efficiency and avoid overflow. In addition, understanding the nature of combinations and mastering efficient calculation methods is crucial to solving many problems in the fields of probability statistics, cryptography, algorithm design, etc.

How to implement adaptive layout of Y-axis position in web annotation? How to implement adaptive layout of Y-axis position in web annotation? Apr 04, 2025 pm 11:30 PM

The Y-axis position adaptive algorithm for web annotation function This article will explore how to implement annotation functions similar to Word documents, especially how to deal with the interval between annotations...

How to elegantly solve the problem of too small spacing of Span tags after a line break? How to elegantly solve the problem of too small spacing of Span tags after a line break? Apr 05, 2025 pm 06:00 PM

How to elegantly handle the spacing of Span tags after a new line In web page layout, you often encounter the need to arrange multiple spans horizontally...

How to make the height of adjacent columns in the Element UI automatically adapt to the content? How to make the height of adjacent columns in the Element UI automatically adapt to the content? Apr 05, 2025 am 06:12 AM

How to make the height of adjacent columns of the same row automatically adapt to the content? In web design, we often encounter this problem: when there are many in a table or row...

Master SQL SELECT statements: A comprehensive guide Master SQL SELECT statements: A comprehensive guide Apr 08, 2025 pm 06:39 PM

SQLSELECT statement Detailed explanation SELECT statement is the most basic and commonly used command in SQL, used to extract data from database tables. The extracted data is presented as a result set. SELECT statement syntax SELECTcolumn1,column2,...FROMtable_nameWHEREconditionORDERBYcolumn_name[ASC|DESC]; SELECT statement component selection clause (SELECT): Specify the column to be retrieved. Use * to select all columns. For example: SELECTfirst_name,last_nameFROMemployees; Source clause (FR

distinct function usage distance function c usage tutorial distinct function usage distance function c usage tutorial Apr 03, 2025 pm 10:27 PM

std::unique removes adjacent duplicate elements in the container and moves them to the end, returning an iterator pointing to the first duplicate element. std::distance calculates the distance between two iterators, that is, the number of elements they point to. These two functions are useful for optimizing code and improving efficiency, but there are also some pitfalls to be paid attention to, such as: std::unique only deals with adjacent duplicate elements. std::distance is less efficient when dealing with non-random access iterators. By mastering these features and best practices, you can fully utilize the power of these two functions.

How to adjust the wordpress article list How to adjust the wordpress article list Apr 20, 2025 am 10:48 AM

There are four ways to adjust the WordPress article list: use theme options, use plugins (such as Post Types Order, WP Post List, Boxy Stuff), use code (add settings in the functions.php file), or modify the WordPress database directly.

See all articles