一尘不染

如何在SQL中按任意条件排序

sql

我有下表:

CREATE TABLE Bable
    (
     id int identity primary key, 
     name varchar(20), 
     about varchar(30)
    );
INSERT INTO Bable (name,about) VALUES
('ООО Name Firm 1','texttexttexttext'),
('ООО Name Firm 2','texttexttexttext'),
('ООО Name Firm 3','texttexttexttext'),
('ООО Name Firm 4','texttexttexttext'),
('ООО Name Firm 5','texttexttexttext'),
('ООО Name Firm $1','texttexttexttext'),
('ООО Name Firm $2','texttexttexttext'),
('ООО Name Firm $3','texttexttexttext'),
('ООО Name Firm 6','texttexttexttext'),
('ООО Name Firm 7','texttexttexttext')

我可以编写如下查询:

SELECT * FROM Bable WHERE about = 'texttexttexttext'

如何更改此查询以返回有序结果,以使那些名称包含“ $”的结果首先出现,然后不出现,每个组然后按name升序排列?

表格的结构在这里


阅读 184

收藏
2021-05-16

共1个答案

一尘不染

SELECT * FROM Bable
ORDER BY CASE WHEN name LIKE ‘%$..’ THEN 0 ELSE 1 END,
Name

2021-05-16