我正在做這個項目的作業。這個想法是建立一個類似於Netflix的網站。如何區分用戶搜索輸入的關鍵字?
我的任務是:
On the "Searching" page, the customer can search for movies by any of the following attributes or their combination (logical "AND" operation):
title;
year;
director;
star's first name and/or last name. This means you need to do both: a) first name or last name if only one of the two names is provided; b) first name and last name, if both names are provided.
我需要查詢我的MySQL數據庫對所有的電影,演員等配套的查詢和吐回了我的網站上。我想知道區分用戶搜索輸入的關鍵字的最佳方式。我最初的想法是用空格分隔用戶的搜索關鍵字,然後嘗試將它們與MySQL表中的每一列進行匹配。
例如,如果用戶搜索「冰凍伊迪娜門澤爾」我會先分析自己的搜索爲單個單詞「凍結」,「伊迪娜」,「門澤爾」,我會這樣說
SELECT * FROM movies WHERE title = 'Frozen' UNION
SELECT * FROM stars WHERE first_name = 'Frozen' UNION
SELECT * FROM stars WHERE last_name = 'Frozen' UNION
SELECT * FROM movies WHERE title = 'Idina' UNION
SELECT * FROM stars WHERE first_name = 'Idina' UNION
SELECT * FROM stars WHERE last_name = 'Idina' UNION
SELECT * FROM movies WHERE title = 'Menzel' UNION
SELECT * FROM stars WHERE first_name = 'Menzel' UNION
SELECT * FROM stars WHERE last_name = 'Menzel'
SELECT * FROM stars WHERE first_name = 'Frozen' AND last_name = 'Idina';
SELECT * FROM stars WHERE first_name = 'Idina' AND last_name = 'Menzel';
但是,這似乎不是一個很好的方法,因爲有很多不必要的查詢,所以我想知道是否有更好的方法來做到這一點。
你有沒有聽說過全文檢索? http://dev.mysql.com/doc/refman/5.5/en/fulltext-search.html –