SQL 如何从表中选择所有列,以及 ROWNUM 等附加列?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3999491/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 08:02:17  来源:igfitidea点击:

How do I select all the columns from a table, plus additional columns like ROWNUM?

sqloracleselect

提问by ntsue

In Oracle, it's possible to do a SELECTstatement that returns the row number as a column in your result set.

在 Oracle 中,可以执行SELECT将行号作为结果集中的列返回的语句。

For example,

例如,

SELECT rownum, column1, column2 FROM table

returns:

返回:

rownum       column1       column2
1            Joe           Smith
2            Bob           Jones

But I don't want to specify each column by hand. I want to do something like:

但我不想手动指定每一列。我想做类似的事情:

select rownum,* from table
rownum       column1       column2       column3       column4
1            Joe           Smith         1             2
2            Bob           Jones         3             4

Any ideas?

有任何想法吗?

回答by Dave Costa

Qualify the * with the name of the table:

使用表名限定 *:

select rownum, table.* from table

回答by gaborous

Dave's answer is great, i'd just like to add that it's also possible to do that by placing the wildcard as the first column:

戴夫的回答很棒,我想补充一点,也可以通过将通配符作为第一列来做到这一点:

select *,rownum from table

Works, but the following won't:

工作,但以下不会:

select rownum,* from table

I've tested on MySQL.

我已经在 MySQL 上进行了测试。

回答by Ziyi Wang

Unfortuantely, i dont think therei s a way to do it, easiest is probably inner join with itself with an inline table of id,count(*), and put an outer select statement

不幸的是,我不认为有办法做到这一点,最简单的方法可能是使用 id,count(*) 的内联表与自身进行内部连接,并放置一个外部选择语句