oracle 如何使用连接'||' 带有 SELECT 查询的不同子句?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3148408/
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-18 20:56:41  来源:igfitidea点击:

How to use concatenation '||' with distinct clause of SELECT query?

sqldatabaseoracle

提问by cctk11

I get a missing expression error when I try,

当我尝试时,我得到一个缺少表达式的错误,

SELECT 'label1:'||distinct col1 from tab1;

Is there a way to get around this error? Thanks in advance.

有没有办法解决这个错误?提前致谢。

Edit: The entire query is:

编辑:整个查询是:

SELECT 'label1:'||distinct col1 from tab1 order by col1;

采纳答案by Bharat

try this one

试试这个

SELECT  DISTINCT 'label1:' || col1
FROM tab1 order by 1;

回答by Martin Smith

The first error is because distinctis in the wrong place.

第一个错误是因为distinct在错误的地方。

SELECT distinct 'label1:'|| col1 as c 
from tab1
ORDER BY c;

The second one mentioned in the comments is because you were ordering by col1. You need to alias the new column and order by the alias as above. (Note you can use col1as the alias if you want)

评论中提到的第二个是因为您是按 col1 订购的。您需要为新列添加别名并按上述别名排序。(请注意col1,如果需要,您可以将其用作别名)

回答by Quassnoi

DISTINCTis a part of a SELECTclause, not the function of the columns:

DISTINCTSELECT子句的一部分,而不是列的功能:

SELECT  DISTINCT 'label1:' || col1
FROM    tab1

Update:

更新:

To make it work with ORDER BY, use

要使其与 一起ORDER BY使用,请使用

SELECT  'label1:' || col1
FROM    (
        SELECT  DISTINCT col1
        FROM    tab1
        )
ORDER BY
        col1