我有以下表格,
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
我想在所有表中搜索给定的关键字。
我尝试使用LIKE,它返回了很多行,
我试过的代码,
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%'
有任何帮助吗?
答案 0 :(得分:0)
如果您想查询完全匹配,请使用=
:
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'
如果要使用" 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_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 %'