Mysql: Order by like?(Mysql:按like排序?)
问题描述
假设我们使用关键字执行搜索:keyword1、keyword2、keyword3
数据库中有name"列的记录:
<前>1:约翰·多伊2:塞缪尔·多伊3:约翰史密斯4:安娜史密斯现在查询:
SELECT * FROM users WHERE (name LIKE "%John%" OR name LIKE "%Doe%")它将选择记录:1,2,3(按此顺序)但我想按关键字订购例如 keyword1=John, keyword2=Doe所以应该按关键字列出:1,3,2(因为我想在搜索John"后搜索Doe")
我在考虑 SELECT DISTINCT FROM (...... UNION .....)但是以另一种方式对其进行排序会容易得多(真正的查询很长)
有什么技巧可以创建这样的订单吗?
order by case当名字 LIKE "%John%" 然后 1当名称 LIKE "%Doe%" 然后 2其他 3结尾assume that we are performing search using keywords: keyword1, keyword2, keyword3
there are records in database with column "name":
1: John Doe 2: Samuel Doe 3: John Smith 4: Anna Smith
now Query:
SELECT * FROM users WHERE (name LIKE "%John%" OR name LIKE "%Doe%")
it will select records: 1,2,3 (in this order)
but i want to order it by keyword
in example keyword1=John, keyword2=Doe
so it should be listed by keywords: 1,3,2 (because i want to perform search for "Doe" after searching for "John")
I was thinking about SELECT DISTINCT FROM (...... UNION .....)
but it will be much easier to order it somehow in another way (real query is really long)
are there any tricks to create such order?
order by case
when name LIKE "%John%" then 1
when name LIKE "%Doe%" then 2
else 3
end
这篇关于Mysql:按like排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Mysql:按like排序?
基础教程推荐
- SQL:使用来自具有相同列名的两个表中的数据... 2021-01-01
- 是否可以执行按位分组功能? 2021-01-01
- SSMS 中的权限问题:“对象 'extended_properties'、数据库 'mssqlsystem_resource'、... 错误 229)上的 SELECT 权限被拒绝" 2022-01-01
- 在 SQL 中连接多个表 2021-01-01
- 将 SQL Server DateTime 列迁移到 DateTimeOffset 2021-01-01
- SQL Server 实例在登录协商期间返回无效或不受支持的协议版本 2021-01-01
- SQL 效率:WHERE IN 子查询 vs. JOIN 然后 GROUP 2021-01-01
- 无法解决整理冲突 2021-01-01
- 如何使用 mysql.connector 禁用查询缓存 2022-01-01
- 需要 MySQL 5.1 中的抽象触发器来更新审计日志 2021-01-01
