我希望有一个查询按行中一个短语的出现次数对我的表进行排序。
像这样:
select * from table where paragraph like '%im%' order by (count of success like statement execute in row)
_________________
| paragraph |
_________________
|hello im vahid|
| and im happy | with 2 (im)
_________________
在这种情况下结果是
_________________
| paragraph |
_________________
|hello im vahid |
|and im happy |
_________________
|im vahid glad |
|to meet u |
_________________
<小时/> 是 Gerrat说得对, 包含字母“im”(在段落列中)的每个记录按每个段落包含字母“im”的次数的降序频率排序
$trimmed1= "my paragraph";
$pieces = explode(" ", trim($trimmed1));
$query = "SELECT * FROM agahi
WHERE active='2' AND ( ";
$countp = count($pieces);
for ($i = 0; $i < $countp-1; $i++) {
$query .=" CONCAT(sharh,' ',onvan,' ',kelid) LIKE '%".$pieces[$i]."%' OR ";
}
$query .=" CONCAT(sharh,' ',onvan,' ',kelid) LIKE '%".$pieces[$i]."%' " ;
$query .= ")
ORDER BY (";
for ($i = 0; $i < $countp-1; $i++) {
$query .=
"(
CASE WHEN CONCAT(sharh,' ',onvan,' ',kelid) LIKE '%".$piecesf[$i]."%'
THEN 1
ELSE 0
END
) + ";}
$query .= "(CASE WHEN CONCAT(sharh,' ',onvan,' ',kelid) LIKE '%".$piecesf[$i]."%'
THEN 1
ELSE 0
END )
) DESC ";
这是我的代码,但ORDER BY不能正常运行
这是生成的代码:
SELECT * FROM agahi WHERE active='2' AND ( CONCAT(sharh,' ',onvan,' ',kelid) LIKE '%cd%' OR CONCAT(sharh,' ',onvan,' ',kelid) LIKE '%dvd%' )
ORDER BY (
( CASE WHEN CONCAT(sharh,' ',onvan,' ',kelid) LIKE '%cd%' THEN 1 ELSE 0 END )
+ ( CASE WHEN CONCAT(sharh,' ',onvan,' ',kelid) LIKE '%dvd%' THEN 1 ELSE 0 END )
) DESC
答案 0 :(得分:1)
select *
from table t
inner join
(select tableID, numberToControlSort
from [put the rest of a query that calcs a priority on each row here]) s
on t.tableID=s.tableID
where [put your where clause here]
order by s.numberToControlSort
这会奏效。不幸的是,SQLServer没有一个库存例程来计算子字符串在字符串中出现的次数。您可以编写/查找执行此操作的函数,并将该函数用于numberToControlSort。
答案 1 :(得分:0)
获取paragraph
值的长度,然后从中删除所有出现的'im'
并获取结果字符串的长度。两个长度之间的差异将为您提供可用于按以下顺序排序结果集的值:
在MySQL中,查询可能如下所示:
SELECT *
FROM atable
WHERE paragraph LIKE '%' + @testString + '%'
ORDER BY
CHAR_LENGTH(paragraph) - CHAR_LENGTH(REPLACE(paragraph, @testString, ''))
其中@testString
是您的'im'
或您要测试paragraph
的任何其他子字符串。
答案 2 :(得分:0)
SELECT name, COUNT(*) as frequency
FROM name_table
GROUP BY name
ORDER BY COUNT(*) DESC
有些人说要用这个。那样有用吗?
答案 3 :(得分:0)
选择 ( CHAR_LENGTH(sharh) - CHAR_LENGTH(REPLACE(sharh,'cd','')) )AS nextlenght
来自agahi
由nextlenght DESC命令