I have the following tables,
state : state_id, state_name
city : city_id, city_name, state_id
category : category_id, category_name
products : product_id, product_name, cateogory_id, product_description, city_id
I want to search all the tables for a given keyword.
I tried using LIKE, which is returning many rows,
The code i tried,
keyword = 'apple';
SELECT
*
FROM
products AS p,
categories AS cat,
city AS c,
state as s
WHERE
p.category_id=cat.category_id
AND p.city_id=c.city_id
AND c.state_id=s.state_id
AND p.product_name LIKE '%apple%'
OR p.product_description LIKE '%apple%'
OR cat.category_name LIKE '%apple%'
OR c.city_name LIKE '%apple%'
OR s.state_name LIKE '%apple%'
Any help?
If you want to query an exact match, use =
:
SELECT
*
FROM
products AS p,
categories AS cat,
city AS c,
state as s
WHERE
p.category_id=cat.category_id
AND p.city_id=c.city_id
AND c.state_id=s.state_id
AND p.product_name = 'apple'
OR p.product_description = 'apple'
OR cat.category_name LIKE = 'apple'
OR c.city_name LIKE = 'apple'
OR s.state_name LIKE = 'apple'
If you want to return all rows with the "apple" word, try to add blanks around it like that :
SELECT
*
FROM
products AS p,
categories AS cat,
city AS c,
state as s
WHERE
p.category_id=cat.category_id
AND p.city_id=c.city_id
AND c.state_id=s.state_id
AND p.product_name LIKE '% apple%'
OR p.product_name LIKE '%apple %'
OR p.product_description LIKE '% apple%'
OR p.product_description LIKE '%apple %'
OR cat.category_name LIKE '% apple%'
OR cat.category_name LIKE '%apple %'
OR c.city_name LIKE '% apple%'
OR c.city_name LIKE '%apple %'
OR s.state_name LIKE '% apple%'
OR s.state_name LIKE '%apple %'