简体   繁体   English

根据列值而不是列名查找订单

[英]Find order by based on column value not column name

I have a query where I want to order based on column value not column name ie 我有一个查询,我想根据列值而不是列名进行排序,即

Suppose I have a column "Description" in table and in my sql I have like condition as 假设我在表中有一列“说明”,而在我的sql中,我的条件是

Select * from tableName 
where Description like '%Bombay%' 
or Description like '%Hotel%' like Description like '%Rent%'

I want to order the records if column Description have all the three values or two values or one value ie if all the three values are in Description column it should be in at top 我想对“描述”列中的所有三个值或两个值或一个值进行排序,即如果所有三个值都在“描述”列中,则该记录应位于顶部

Is it possible in sql ? 有可能在sql中吗?

drop table if exists dbo.tableName;


create table dbo.tableName (
ID int primary key
, Description varchar(100)
);

insert into dbo.tableName (ID, Description)
values (1, 'Hotel Bombay Rent')
, (2, 'Hotel Bombay')
, (3, 'Hotel')
, (4, 'Aparment');



select
t.ID
, t.Description
from dbo.tableName t
order by (case when t.Description like '%Hotel%' then 1 else 0 end)
+ (case when t.Description like '%Bombay%' then 1 else 0 end)
+ (case when t.Description like '%Rent%' then 1 else 0 end) desc

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

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