2011-07-13 18 views
1

私のサイトには、製品名とブランド名を検索するためのページ設定機能があります。私は、ユーザの検索要求を取得するには、このクエリを使用しています:ウェブサイト上でページ設定を使用して検索機能を使用する

SELECT * 
from products 
WHERE name = 'optimum nutrition' 
    OR brand = 'optimum nutrition' 
    OR name LIKE '%optimum%' 
    OR brand LIKE '%optimum%' 
    OR name LIKE '%nutrition%' 
    OR brand LIKE '%nutrition%' 

私は最初のブランド名のいずれかで、製品名に完全な「最適な栄養」を持っている製品を表示したいと思います。私はこれをどのように達成するでしょうか?

ご意見をいただければ幸いです。

+0

あなたのクエリが正常に動作する必要があり、それに何か問題があるのでしょうか? – Starx

答えて

2

:私はname = 'optimum nutrition'もなると信じ

SELECT *, 
CASE WHEN (name = 'optimum nutrition' OR brand = 'optimum nutrition') THEN 1 ELSE 0 END AS full_match, 
CASE WHEN (name LIKE '%optimum%' OR brand LIKE '%optimum%' OR name LIKE '%nutrition%' OR brand LIKE '%nutrition%') THEN 1 ELSE 0 END AS half_match 
FROM products 
WHERE (name = 'optimum nutrition' OR brand = 'optimum nutrition') 
OR (name LIKE '%optimum%' OR brand LIKE '%optimum%' OR name LIKE '%nutrition%' OR brand LIKE '%nutrition%') 
ORDER BY full_match, half_match 
0

あなたのページに検索機能を追加するには、 Zend Search Luceneを調べることをお勧めします。

0

私はMySQL full text searchを調べることをお勧めします。これは、あなたが検索したい列にFULLTEXTインデックスを追加し、このようになります。そのクエリを使用することを含む:

てみ
SELECT * 
FROM products 
WHERE MATCH(name, brand) AGAINST ('optimum') 
関連する問題