普适数据库order by语句

biswetbf  于 2021-07-26  发布在  Java
关注(0)|答案(2)|浏览(283)

我有以下sql语句:

select * 
from
    (select  
         id,
         left(id, 6) as Hauptnummer,
         convert(substring(id, 8), SQL_BIGINT) as Detailnummer
     from 
         Proben 
     where 
         id like '%-%'  
         and id like '2%' 
         and length(id) > 7) as a 
order by  
    Hauptnummer desc, Detailnummer

运行此语句时出现以下错误:
![odbc引擎接口]表达式错误
语句在没有 ORDER BY 条款。
我怎么写这个 ORDER BY 条款?

sr4lhrrt

sr4lhrrt1#

您正在尝试按子查询之外的字段排序。让我们试试同样的代码(下面是ms sql的语法):

/* declare and create temp (in-memory) table */
declare @proben table
(
    id nvarchar(24)
)

/* insert come test data to table */
insert @proben(id) values('2345-67890')
insert @proben(id) values('54654645-6257890')
insert @proben(id) values('2345-67890546541')
insert @proben(id) values('4355343542345-67890')

/* check of select of your script */
select *
from (
    select
        id,
        left(id, 6) as Hauptnummer,
        CAST(substring(id, 8, 0) AS BIGINT) as Detailnummer
    from
        @proben
    where
        id like '%-%'
        and id like '2%'
        and len(id) > 7
    order by
        Hauptnummer desc, Detailnummer
    ) as a
order by
    a.Hauptnummer desc, a.Detailnummer
brqmpdu1

brqmpdu12#

如果在sqlserver(t-sql)中使用的代码可以通过以下方法实现:substring函数接受字符转换,因此代码已按以下方式重新编写。子字符串函数的语法: SUBSTRING(string, start, length) 例如:

select * 
from
    (select  
        id,
        left(id, 6) as Hauptnummer, 
        substring(convert (varchar(20), id ), 1, 2) as Detailnummer 
     from 
        Proben 
     where 
        id like '%-%'  
        and id like '2%'  
        and len(id) > 7) as a  /* SQL server it is len instead length */
order by  
    Hauptnummer desc, Detailnummer

相关问题