Oracle 数据库:如何选择所有但首先返回某些列?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9727018/
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
Oracle database: how to select all but return certain columns first?
提问by SeanKilleen
Background
背景
I have an oracle database table with a lot of columns that I'm running some queries on.
我有一个包含很多列的 oracle 数据库表,我正在对其运行一些查询。
I don't know exactly what data I'm looking for in my query, so I want to return all columns, but I don't want to hunt and peck for columns I know are meaningful.
我不知道我在查询中究竟要查找什么数据,所以我想返回所有列,但我不想寻找和啄食我知道有意义的列。
Question
题
Supposing a table (Table 1) with Column A, Column B, Column C....Column Z--
假设一个表(表 1)包含A 列、B 列、C 列....Z 列-
Is there a way to essentially say "Select Column C, Column J, Column F, Column Q, and then the rest of the columns From Table 1" ?
有没有办法从本质上说“选择 C 列、J 列、F 列、Q 列,然后是表 1 中的其余列”?
Things I've Tried
我尝试过的事情
Keeping with pseudo-sql, Running:
与伪 sql 保持一致,运行:
Select Column C, Column J, Column F, Table1.* from Table1
从表 1 中选择 C 列、J 列、F 列、表 1.*
Doesn't help, because even though I don't mind the duplicates, oracle sees them as ambiguously defined columns and thus returns an error.
没有帮助,因为即使我不介意重复,oracle 将它们视为定义不明确的列,因此返回错误。
回答by GolezTrol
There is no nice and easy way to do this, other than specifying each column.
除了指定每一列之外,没有什么好的和简单的方法可以做到这一点。
But if you don't mind the duplicates andyou don't care about the column names, you could alias those columns:
但是,如果您不介意重复项并且不关心列名,则可以为这些列设置别名:
Select
ColumnC as ColumnC1,
ColumnJ as ColumnJ1,
ColumnF as ColumnF1,
t.*
from
Table1 as t
Just for demonstration, I aliased Table1 as well. You may omit the as
keyword, but I feel it makes it a little more readable.
只是为了演示,我也给 Table1 起了别名。你可以省略as
关键字,但我觉得它使它更具可读性。
Do note that while these extra columns are not at all difficult for Oracle to query, they do generate extra traffic. For testing, this solution is fine, but in production code, I would opt to only select the columns you need, and only select them once. It's only a little extra work. After all, how many columns do you have? :)
请注意,虽然这些额外的列对于 Oracle 来说并不难查询,但它们确实会产生额外的流量。对于测试,这个解决方案很好,但在生产代码中,我会选择只选择你需要的列,并且只选择一次。这只是一点点额外的工作。毕竟,你有多少列?:)
回答by Justin Cave
You can work around the problem, however, by aliasing the columns that you are specifically selecting. For example
但是,您可以通过为您专门选择的列设置别名来解决该问题。例如
SELECT columnC new_columnC, columnJ new_columnJ, columnF new_columnF, t.*
FROM table1 t
to ensure that there are no identically named columns.
以确保没有同名的列。