简体   繁体   English

Oracle数据库:如何选择所有但首先返回某些列?

[英]Oracle database: how to select all but return certain columns first?

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将它们视为模糊定义的列,因此返回错误。

There is no nice and easy way to do this, other than specifying each column. 除了指定每列之外,没有简单易行的方法。

But if you don't mind the duplicates and you 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. 仅仅为了演示,我也将Table1别名。 You may omit the as keyword, but I feel it makes it a little more readable. 您可以省略as关键字,但我觉得它使它更具可读性。

Do note that while these extra columns are not at all difficult for Oracle to query, they do generate extra traffic. 请注意,虽然这些额外的列对于Oracle来说并不困难,但它们确实会产生额外的流量。 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? 毕竟,你有多少列? :) :)

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. 确保没有相同名称的列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM