【问题标题】:SQL order by hierachical likeSQL order by hierarchical like
【发布时间】:2018-03-26 10:53:57
【问题描述】:

我正在使用一个 Confluence 网页,它显示一个基于数据库功能的下拉列表。这个函数没有参数。该函数返回视图的结果。

当我开始在文本字段中输入一些文本时,下拉列表结果显示数据排序。

按实例:

text entered : toto
dropdown list list result : 

aaa.toto.aim
aaa.toto.becare
toto
toto.aim
toto.aim.thisis
toto.becare
toto.becare.xxx

视图有多个列:code_part1、code_part2、code_part3、fullcode(对应结果显示,code_part1 + '.' + code_part2 + '.' + code_part3 串联):

fullcode            |    code_part1    |     code_part2      | code_part3
aaa.toto.aim        |    aaa           |     toto            | aim
aaa.toto.becare     |    aaa           |     toto            | becare
toto                |    toto          |     (null)          | (null)
toto.aim            |    toto          |     aim             | (null)
toto.aim.thisis     |    toto          |     aim             | thisis
toto.becare         |    toto          |     becare          | (null)
toto.becare.xxx     |    toto          |     becare          | xxx

根据我在文本框中输入的内容(此处为“toto”),如何使用 order by 子句更新函数:

toto
toto.aim
toto.aim.thisis
toto.becare
toto.becare.xxx
aaa.toto.aim
aaa.toto.becare

【问题讨论】:

  • 感谢大家的快速反馈。对不起,也许我不好解释我的问题。在 SQL Server 端,我们不知道过滤器,因此,我们不能真正使用搜索字符串添加订单,因为它还不存在。在 Web 服务器端,函数是启动(我想),并且 LIKE 语句似乎适用。因此,显示的结果是我们认为已在系统中输入的视图上的数据。
  • 混淆是由您选择的标签引起的 - 所有这些都适用于 dbms。您打算在哪里实现此功能?此外,像“我想”这样的词也表明你的困惑。使用MVCE 澄清您的问题,以提高您获得有用建议的机会。
  • 感谢您的反馈。我更新我的帖子。我使用带有文本框的融合网页,该文本框显示按第一列排序的简单函数(返回视图结果)的结果。但我想更新功能。问候

标签: sql sql-server sql-like sql-order-by


【解决方案1】:

你可以按顺序使用大小写

DECLARE @SearchString CHAR ='toto'
SELECT fullcode 
FROM TABLENAME
ORDER BY CASE WHEN part1 = @SearchString THEN part1  WHEN part2 = @SearchString THEN PART2 ELSE part3 END

【讨论】:

    【解决方案2】:

    试试这个,

    declare @t table(fullcode varchar(50),code_part1  varchar(50), code_part2  varchar(50), code_part3  varchar(50))
    insert INTO @t VALUES
    ('aaa.toto.aim'        ,'aaa'    ,    'toto' ,              'aim'  )
    ,('aaa.toto.becare'     ,'aaa'    ,    'toto'  ,          'becare'  )
    ,('toto'                ,'toto'   ,    null ,         null        )
    ,('toto.aim'            ,'toto'   ,    'aim'    ,         null    )
    ,('toto.aim.thisis'     ,'toto'   ,    'aim' ,            'thisis' )
    ,('toto.becare'         ,'toto'   ,    'becare'   ,      null     )
    ,('toto.becare.xxx'     ,'toto'   ,    'becare',           'xxx'      )
    
    declare @key varchar(50)='toto'
    
    select *,1 rownum from @t where code_part1=@key
    union ALL
    select *,2 rownum from @t where code_part2=@key
    union ALL
    select *,3 rownum from @t where code_part3=@key
    

    【讨论】:

      猜你喜欢
      • 2013-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多