Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server T-SQL:以列中搜索词最多的结果首先显示的方式显示结果_Sql Server_Tsql_Select_Search - Fatal编程技术网

Sql server T-SQL:以列中搜索词最多的结果首先显示的方式显示结果

Sql server T-SQL:以列中搜索词最多的结果首先显示的方式显示结果,sql-server,tsql,select,search,Sql Server,Tsql,Select,Search,例如,我执行了以下查询: Select * From MyTable Where Description like '%Leave%' 它将返回以下3行: | ID | Description | |:---|------------:| | 1 | What is **Leave**? | | 2 | How to avail the **Leave**? where as I am not entitled to **leave**, and **leave** is r

例如,我执行了以下查询:

Select * 
From MyTable 
Where Description like '%Leave%'
它将返回以下3行:

| ID | Description |
|:---|------------:|
| 1  | What is **Leave**? |     
| 2  | How to avail the **Leave**? where as I am not entitled to **leave**, and **leave** is required. |    
| 3  | Why **Leave** type is not showing in **Leave** type collection. |     
  • ID 1包含计数为1的单词“Leave”
  • ID 2包含计数为3的单词“Leave”
  • ID 3包含计数为2的单词“Leave”
问题是,我希望以一种方式显示结果,即首先显示“Description”列中搜索词最多的结果。当我通过“Leave”搜索时,包含单词“Leave”的行应该首先出现


我是否可以通过按顺序选择查询?

试试这个:

WITH CTE AS(

SELECT *, LEN([Description]) - LEN(REPLACE([Description],'Leave','')) as CNT
FROM MyTable 
WHERE [Description] like '%Leave%'
)
SELECT * 
FROM CTE 
ORDER BY CNT DESC
或者不使用CTE

SELECT *
FROM MyTable 
WHERE [Description] like '%Leave%'
ORDER BY LEN([Description]) - LEN(REPLACE([Description],'Leave','')) DESC