【问题标题】:JQuery Autocomplete searching for term and its abbreviationJQuery自动完成搜索术语及其缩写
【发布时间】:2017-06-21 02:56:37
【问题描述】:

使用 JQuery 的 UI 自动完成和使用糟糕的数据库模式可以搜索一个术语的开头和它的缩写?

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

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

用户如何输入 Saint 并返回 SaintSt

我想到了这个简单的 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 %');

我的问题有解决方案吗?

【问题讨论】:

  • WHERE School_Name LIKE '%Sain%' OR School_Name LIKE 'St %' no?
  • 查询是否完成了工作?
  • 它没有。匹配并返回一个,但不能同时返回。

标签: php jquery mysql autocomplete


【解决方案1】:

我的解决方案:

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');
    }

【讨论】:

    【解决方案2】:

    在sql中这样使用

    使用 CHARINDEX()

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

    希望它的作品!!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-20
      • 1970-01-01
      • 2011-04-26
      • 2016-11-12
      • 1970-01-01
      • 2015-08-18
      相关资源
      最近更新 更多