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”
我是否可以通过按顺序选择查询?试试这个:
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