mysql - 按一列与另一列的值完全相同的位置排序

标签 mysql sql-order-by

我需要根据一列与另一列的值完全相同的位置进行排序。有什么办法可以做到这一点?如果有,请告诉我。 ORDER BY wordMatch = wordCount

SELECT * , 
( input LIKE  '% i %') + 
( input LIKE  '% love %' ) + 
( input LIKE  '% you %' ) AS wordMatch, 
( LENGTH( input ) - LENGTH( REPLACE( input,  ' ',  '' ) ) -1 ) AS wordCount
FROM allData
HAVING wordMatch > ( wordCount * 0.6666 ) 
AND wordCount > ( 3 * 0.6666 ) 
ORDER BY wordMatch = wordCount
LIMIT 50

最佳答案

如果您首先想要最接近的匹配项,则使用 desc:

order by (wordMatch = wordCount) desc

或者,您可能还需要差值的绝对值:

order by abs(wordMatch - wordCount)

关于mysql - 按一列与另一列的值完全相同的位置排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33818430/

相关文章:

mysql - MySQL表有多少列

mysql无法使用mysql if condition in order by

sql-server - SQL Server : ORDER BY in subquery with UNION

PHP、SQL新手帮助

mysql - 使用 TIMESTAMPDIFF 创建表 mysql

java - 如果我想让我的 nvarchar 类型在 sql server 中按与它们在 java 中使用 unicode 时相同的顺序排序,应该使用什么排序规则

ruby-on-rails - order_by 在每个 rails 3.1 block 中

postgresql - Postgres 不使用 "ORDER BY"的索引

php - 同步 PHP 和 MySQL 时区

java - 如何在你的库中使用 Spring-Data 以及如何在你的项目中使用这个库?