0

嗨,我正在寻找一个想法,通过按 id、搜索词和最畅销的过滤来混合搜索结果(用于 jQuery 自动完成)。我有“产品”表,其中包含以下列:“id,name,...,sold” 这是我的 sql

    SELECT * FROM products
    WHERE 
        state=1 
        AND ( 
            id='$search' OR id LIKE '$search%' OR  id LIKE '%$search%' OR 
            name='$search' OR name LIKE '$search%' OR name LIKE '%$search%' 
            ) 
        ORDER BY ???

在此查询中不包括“已售(已售商品)”。我也在寻找将其合并到我的查询中的方法。我需要这个订单

1. full id (e.g. sombody types 312 than the first row must be id=312)
2. full name (...)
3. otherwise search-pattern combined with "sold"
4

1 回答 1

0

你可以做这样的事情

SELECT p.*, 
       MAX(3 * (id = '$search' OR id LIKE '%$search%'),
           2 * (name LIKE '%312%'),
           1 * (sold LIKE '%312%')) rank 
  FROM products p
 WHERE state = 1 
   AND (id = '$search' OR id LIKE '%$search%'
    OR name LIKE '%$search%'
    OR sold LIKE '%$search%')
 ORDER BY rank DESC

这是SQLFiddle演示

于 2013-08-17T07:27:58.137 回答