简体   繁体   English

SQL如果该值类似于多个列,则选择该列

[英]SQL Select the column if the value is like with multiple columns

I need to create a autocomplete search with ajax. 我需要使用ajax创建自动完成搜索。 The suggestions should only contain the 10 most entered results. 建议仅包含输入最多的10个结果。 The search query has to check multiple columns if the value is like my variable. 如果值类似于我的变量,搜索查询必须检查多列。

But my problem is to create the query and the php logic for that. 但是我的问题是为此创建查询和PHP逻辑。

  1. Is there any plugin or something simular for that? 是否有任何插件或类似的东西?
  2. How can I select a column if the value in it is like my variable? 如果列中的值像我的变量,该如何选择?
  3. I need to create a count query, which counts (in all columns) "how often is here the full word (splitted by spaces)" <- which is like the found one (to get the relevance) 我需要创建一个计数查询,该计数(在所有列中)计数“完整单词在这里有多久(由空格分隔)” <-就像找到的那个(获取相关性)

At the end I need to sort the found entries by their relevance to provide the 10 most relevant entries. 最后,我需要按找到的条目的相关性对它们进行排序,以提供10个最相关的条目。

(The real query checks for more columns than just 2, but for dummy reasons are 2 okay) (实际查询检查的列不只是2个,但出于虚假原因,还可以2个)

The query which selects the rows where the value is like... 选择值类似于的行的查询...

select * from 
(
    (select department from entries where department like '%myVariable%') 
OR 
    (select grade from entries where grade like '%myVariable%')
)

I think you know what I mean. 我想你知道我的意思。 Does anyone have any hints, suggestions, examples or useful links for me? 有人对我有任何提示,建议,示例或有用的链接吗?

Thanks in advance! 提前致谢!

Best regards, 最好的祝福,
FriaN FriaN

Why not use union all here? 为什么不在这里使用工会?

select department from entries where department like '%myVariable%'
union all
select grade from entries where grade like '%myVariable%'

Then this should order the results for you: 然后,这应该为您订购结果:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM