我有用户表(例如 UserMst)是 mysql 之类的
UserID FirstName LastName FullName (expression column)
------ --------- -------- ----------------------------
1 Manish Patel Manish Patel
2 Dharmesh Patel Dharmesh Patel
3 Patel Pranay Patel Pranay
4 Patel Ankita Patel Ankita
现在我正在获取结果如下:
Select * from UserMst
where ( FirstName like 'patel dha%'
OR LastName like 'patel dha%
OR FullName like '%patel dha%
OR FullName like '%patel%
OR FullName like '%dha% )
Order by ( case when FirstName like 'patel dha%' then 1
WHEN LastName like 'patel dha%' then 2
WHEN FirstName like '%patel dha' then 3
WHEN LastName like '%patel dha%'then 4 end ),
FullName
此查询返回结果完美,但我需要更接近的排序结果。在这种情况下,如果有人输入关键字“patel dha”(这里用户可以在 2 个单词之间留出更多空间,并且他也可以输入尽可能多的单词,例如“patel dha”或“patel dha pran”),这意味着他正在尝试查找/搜索用户 1(例如 Dharmesh patel) 此记录应放在首位。
我的预期结果是:(如果用户输入此关键字:“patel dha”或“patel dha”)
UserID FirstName LastName FullName
------ --------- -------- --------
2 Dharmesh Patel Dharmesh Patel
4 Patel Ankita Patel Ankita
3 Patel Pranay Patel Pranay
1 Manish Patel Manish Patel
如果用户以另一种方式搜索,那么我的预期结果是:(如果用户输入此关键字:“patel dha pr”或“patel dha pran”)
UserID FirstName LastName FullName
------ --------- -------- --------
2 Dharmesh Patel Dharmesh Patel
3 Patel Pranay Patel Pranay
4 Patel Ankita Patel Ankita
1 Manish Patel Manish Patel
顺序变了。(因为找到了最匹配的词)
我们可以在这里使用正则表达式吗?或者可能是另一种方式来做到这一点?
谢谢