2012-12-14 31 views
-1

我需要在hostel表中搜索。使用LIKE作爲2列

結構:

CREATE TABLE hostel( 
p_id VARCHAR(5) NOT NULL, 
hostel VARCHAR(50) NOT NULL, 
address VARCHAR(50) NOT NULL 
PRIMARY KEY(p_id)); 

我已經插入搜索詞$word

select * from hostel where hostel like '%$word%' 

將在下面的查詢工作?我需要在這兩個 「宿舍」 和 「地址」 欄搜索

select * from hostel where hostel like '%$word%' AND address like '%$word%' 
+5

爲什麼不你只是測試它? – Sirko

+0

這取決於你想要什麼。您的查詢只會導致旅館,其中旅館和地址的文字$字。我想你想要的是將AND更改爲OR,因此當宿舍或地址包含$ word時,它會返回行。 – Tom

回答

2
select * from hostel where hostel like '%$word%' OR address like '%$word%' 

會帶來更好的效果

0

這工作沒有任何問題

select * from hostel where hostel like '%$word%' AND address like '%$word%' 
2
Better change those searchable fields(hostel,address) to full text which will fasten the search 

SELECT MATCH('Content') AGAINST ('keyword1 
keyword2') as Relevance FROM table 

select match($word) against (hostel,address) as Relevance FROM table