php - SQL 如果值与多列类似,则选择列
问题描述
我需要使用 ajax 创建一个自动完成搜索。建议应仅包含 10 个输入最多的结果。如果值类似于我的变量,则搜索查询必须检查多个列。
但我的问题是为此创建查询和 php 逻辑。
- 是否有任何插件或类似的东西?
- 如果列中的值像我的变量,我该如何选择?
- 我需要创建一个计数查询,它计算(在所有列中)“这里多久出现一次完整的单词(由空格分隔)” <-就像找到的一样(以获得相关性)
最后,我需要按相关性对找到的条目进行排序,以提供 10 个最相关的条目。
(真正的查询检查的列多于 2 列,但出于虚拟原因是 2 还可以)
选择值所在行的查询...
select * from
(
(select department from entries where department like '%myVariable%')
OR
(select grade from entries where grade like '%myVariable%')
)
我想你知道我的意思。有人对我有任何提示、建议、示例或有用的链接吗?
提前致谢!
最好的问候,
弗里安
解决方案
为什么不在这里使用 union all 呢?
select department from entries where department like '%myVariable%'
union all
select grade from entries where grade like '%myVariable%'
然后这应该为您订购结果:
select department, count(*) cnt from (
select department from entries where department like '%myVariable%'
union all
select grade from entries where grade like '%myVariable%')a
group by department
order by count(*) desc
推荐阅读
- dataframe - 在添加新列时填写 pySpark 数据框中的特定行
- mysql - 通过 NodeJS + MySQL 登录时捕获异常错误
- php - 使用while循环获取数据库结果时,如何跳转到第三行并获取数字
- mapbox - 地图框不显示多边形
- ubuntu - 在 ubuntu 18.04 中取消隐藏工作区切换器
- c# - 使用 StreamWriter Async 时出现错误“流当前正在被流上的先前操作使用”
- javascript - ajax 执行后 firefox 将所有表单字段显示为错误
- asp.net - 类似于 Asp.Net Core Razor Pages 中 WebForms 的 LoginView
- c++ - View 如何在幕后更新?
- azure - 如何使用 Httptrigger 函数每 5 分钟调用一次 functionapp?