Ati*_*din 1 mysql search join relevance
任何人都可以帮助我按照以下标准按相关性排序行吗?
`tbluser`
- - - - - - -
First Name
Last Name
`tbleduc`
- - - - - - -
School
College
University
Run Code Online (Sandbox Code Playgroud)
在搜索表单上,用户具有以下字段
Name
School
College
University
Run Code Online (Sandbox Code Playgroud)
学院和大学可选的地方..
并且Name被分成2个单词(中间的其他单词被省略),第一个单词被视为第一个anme而最后一个单词被作为姓氏.
现在我想基于相关性实现搜索.
谢谢您的帮助 :)
好的,我在Postgres上为你试过这个(我这里没有MySQL,所以也许它有点不同):
select matches.id,
(matches.tfirstname
+ matches.tlastname
+ matches.tschool
+ matches.tcollege
+ matches.tuniversity) as total
from (
select u.id,
(case when u.firstname like '%a%' then 1 else 0 end) as tfirstname,
(case when u.lastname like '%b%' then 1 else 0 end) as tlastname,
sum(e2.nschool) as tschool,
sum(e2.ncollege) as tcollege,
sum(e2.nuniversity) as tuniversity
from tbluser u left outer join (
select e.usr,
(case when e.school like '%c%' then 1 else 0 end) as nschool,
(case when e.college like '%d%' then 1 else 0 end) as ncollege,
(case when e.university like '%e%' then 1 else 0 end) as nuniversity
from tbleduc e
) e2 on u.id=e2.usr
group by u.id, u.firstname, u.lastname
) as matches
Run Code Online (Sandbox Code Playgroud)
我使用这些DDL语句来创建表:
create table tbluser (
id int primary key,
firstname varchar(255),
lastname varchar(255)
)
create table tbleduc (
id int primary key,
usr int references tbluser,
school varchar(255),
college varchar(255),
university varchar(255)
)
Run Code Online (Sandbox Code Playgroud)
还有一些示例数据:
insert into tbluser(id, firstname, lastname)
values (1, 'Jason', 'Bourne');
insert into tbleduc(id, usr, school, college, university)
values (1, 1, 'SomeSchool', 'SomeCollege', 'SomeUniversity');
insert into tbleduc(id, usr, school, college, university)
values (2, 1, 'MoreSchool', 'MoreCollege', 'MoreUniversity');
Run Code Online (Sandbox Code Playgroud)
该查询可被简化了一下,如果之间的关系tbluser和tbleduc为1:1.
不要忘了更换%a%,%b你的变量,......(我建议使用事先准备好的声明).
我希望这个模板有助于作为一个基本的解决方案 - 你可以随意调整它:-)你也可以删除最外面的select语句,以获得各个结果的计数器.
| 归档时间: |
|
| 查看次数: |
2093 次 |
| 最近记录: |