SQL在表的最左边显示指定列的查询结果。

huangapple go评论62阅读模式
英文:

SQL display query results with specified columns appearing on the leftmost side of the table

问题

我需要显示SQL查询结果,使指定的列首先出现在表的最左边。

我目前的代码如下:

Select * from table
order by column 4, column 3, column 1

结果:

column 1, column 2, column 3, column 4 - 但它首先按照列4中的结果排序。

我想要的结果使用相同的输入是:

column 4, column 3, column 1, column 2

我不在乎记录显示的顺序,只关心列4首先出现。

英文:

I need to display SQL query results by having specified columns appear first (on the leftmost side of the table).

The currrent code I have is:

Select * from table
order by column 4, column 3, column 1

Result:

column 1, column 2, column 3, column 4 - however it orders by the results in column 4 first.

The result I am looking for when using the same input is:

column 4, column 3, column 1, column 2

I do not care in which order the records are shown, I only care that column 4 appears first.

答案1

得分: 1

指定列名,而不是使用 select *。这样可以确保返回的列按照在 CREATE TABLE 语句中的顺序排列。

Select column4, column3, column1, column2
from table
order by column4, column3, column1
英文:

Specify the columns explicitly instead of using select *. That always returns the columns in the order they appear in the CREATE TABLE statement.

Select column4, column3, column1, column2
from table
order by column4, column3, column1

答案2

得分: 0

ORDER BY子句不是指每列出现的顺序,而是指列中数值的顺序。例如,如果你有一个ORDER BY height, weight子句,那么height会按升序排列,然后是weight。然而,这不会影响哪个列会首先出现在表中。

如果你想确保第4列首先出现在表中,那么在查询中指定它就可以了,即:

select column_4, column_3, column_2, column_1 from table;

英文:

The ORDER BY clause is not referring to the order in which each column appears. Rather, it refers to the order of the values in the column itself. e.g. if you had an ORDER BY height, weight clause, then height would be ordered in ascending order, followed by weight. However, this would not affect which column would appear in the table first.

If you wanted to ensure that column 4 was first in the table, then it is simply a matter of designating it as such in the query, i.e:

select column_4, column_3, column_2, column_1 from table;

huangapple
  • 本文由 发表于 2023年2月16日 04:51:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/75465324.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定