简体   繁体   English

如何在DB2查询中选择特定字段并加星号?

[英]How do you select a specific field and star in a DB2 query?

I want to view all columns in a DB2 database query however I want to put a certain column at the front. 我想查看DB2数据库查询中的所有列,但是我想在前面放一列。

For example from my sql knowledge I would write something like 例如从我的SQL知识,我会写类似

SELECT Field2, * FROM Table

However the above query is returning an error like below 但是上述查询返回如下错误

SQL State: 42601
Vendor Code: -104
Message: [SQL0104] Token * was not valid. Valid tokens: ( + - ? : DAY INF NAN RID ROW RRN CASE CAST CHAR DATE DAYS HASH. Cause . . . . . :   A syntax error was detected at token *.
Token * is not a valid token.  A partial list of valid tokens is ( + - ? : DAY INF NAN RID ROW RRN CASE CAST CHAR DATE DAYS HASH.
This list assumes that the statement is correct up to the token.
The error may be earlier in the statement, but the syntax of the statement appears to be valid up to this point. Recovery  . . . :
Do one or more of the following and try the request again: -- Verify the SQL statement in the area of the token *.
Correct the statement.  The error could be a missing comma or quotation mark, it could be a misspelled word, or it could be related to the order of clauses.
-- If the error token is <END-OF-STATEMENT>, correct the SQL statement because it does not end with a valid clause.

Processing ended because the highlighted statement did not complete successfully

You can do this using a correlation name, like so: 您可以使用相关名称来执行此操作,如下所示:

SELECT Field1, tbl.* FROM YourTable tbl

Obviously, tbl.* returns all columns, so Field1 will appear twice in the result. 显然, tbl.*返回所有列,因此Field1将在结果中出现两次。

You can use a view as below; 您可以使用以下视图;

user@host:/home/db2inst1:>db2 "select * from mytable"

VAL NEW_VAL
--- -------
5   -      
6   -      
A   -      

  3 record(s) selected.

user@host:/home/db2inst1:>db2 "CREATE VIEW MYTABLEVW AS SELECT NEW_VAL,VAL FROM MYTABLE"
DB20000I  The SQL command completed successfully.

user@host:/home/db2inst1:>db2 "select * from mytablevw"

NEW_VAL VAL
------- ---
-       5  
-       6  
-       A  

  3 record(s) selected.

i'm afraid, you'll have to select each column separately. 恐怕您将不得不分别选择每一列。 like 喜欢

select field_you_want_to_appear_first, field2, filed3.. from yourtable

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

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