嗨,我在寻找一个想法来混合搜索结果(对于jQuery自动完成),过滤由id,搜索-word和大多数销售。我有一个"products“表,其中有以下列:"id,name,.,sql”,这里是我的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"发布于 2013-08-17 07:27:58
你可以做这样的事
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演示
https://stackoverflow.com/questions/18284451
复制相似问题