JQuery自动完成搜索术语及其缩写

时间:2017-02-03 16:58:39

标签: php jquery mysql autocomplete

使用JQuery的UI自动完成功能并使用可怕的数据库模式可以搜索术语的开头及其缩写吗?

假设我的数据库中有以下教会:

St Ignatius
St Mary of Sacred Heart
Saint Joseph
Saint Peter the Apostle
St Peter First Lutheran Church

用户如何输入 Saint 并返回 Saint St

我想到了这个简单的SQL UNION:

SELECT School_Name FROM mi_firstyear_advisors WHERE (School_Name LIKE '%Sain%')
UNION
SELECT School_Name FROM mi_firstyear_advisors WHERE (School_Name LIKE 'St %');

我的问题有解决方案吗?

2 个答案:

答案 0 :(得分:0)

在sql中使用就像那样

使用CHARINDEX()

DECLARE @Name nvarchar(max)
Begin
SELECT School_Name FROM mi_firstyear_advisors WHERE CHARINDEX(@Name,School_Name)
End

希望它的作品!!

答案 1 :(得分:0)

我的解决方案:

switch($term){
        // If any variant of 'saint' is entered, search for any variant of 'saint'
        case (preg_match('/st .*/', $term) ? true : false):
        case (preg_match('/st\..*/', $term) ? true : false):
        case (preg_match('/sai.*/', $term) ? true : false):
        $stmt = $conn->prepare('SELECT School_Name FROM mi_firstyear_advisors WHERE School_Name LIKE ":term" OR School_Name LIKE "st %" OR School_Name LIKE "st. %" OR School_Name LIKE "%saint%"');
        break;

        // else just look for the given term
        default:
        $stmt = $conn->prepare('SELECT School_Name FROM mi_firstyear_advisors WHERE School_Name LIKE :term');
    }