SQL Server:按名称排序的正确方法

时间:2021-07-27 22:49:07

I have a query containing lines like the following:

我有一个包含以下行的查询:

SELECT    B.LastName + ', ' + B.FirstName AS TM

Currently I use ORDER BY TM in order to sort, which seems to work.

目前我使用ORDER BY TM进行排序,这似乎有效。

However, in order to do it right, can someone tell me what is the correct way here to order by TM?

但是,为了做到正确,有人可以告诉我这里订购TM的正确方法是什么?

ORDER BY TM,  
ORDER BY B.TM,  
ORDER BY B.LastName + ', ' + B.FirstName 

Or something else?

或者是其他东西?

Also, can you tell me if I need to put the TM in single quotes?

另外,你能告诉我是否需要将TM放在单引号中吗?

It seems to work without the quotes but I want to make sure I do this right.

它似乎没有引号,但我想确保我这样做。

1 个解决方案

#1


1  

ORDER BY TM

Best way!

ORDER BY B.TM,

No, TM is not one of B's columns.

不,TM不是B的专栏之一。

ORDER BY B.LastName + ', ' + B.FirstName

Can be done, but first alternative is so much easier.

可以做到,但第一种选择更容易。

Qutes are required for reserved words, and for identifiers including strange characters (incl space).

保留字和标识符包括奇怪的字符(包括空格)需要Qutes。

#1


1  

ORDER BY TM

Best way!

ORDER BY B.TM,

No, TM is not one of B's columns.

不,TM不是B的专栏之一。

ORDER BY B.LastName + ', ' + B.FirstName

Can be done, but first alternative is so much easier.

可以做到,但第一种选择更容易。

Qutes are required for reserved words, and for identifiers including strange characters (incl space).

保留字和标识符包括奇怪的字符(包括空格)需要Qutes。