我有 2 个表、产品和类别定义如下:
表:类别 - category_id,类别
表:产品 - product_id、category_id、item_no、description
我正在尝试将描述和类别连接到一个可搜索的字段中。当我执行以下操作时,它可以工作,但是代码很笨重:
SELECT *, concat(description, ' ',category) as searchable
FROM products, categories
where concat(description, ' ',category) like '%flowers%'
and concat(description, ' ',category) like '%rainbow%'
and products.category_id=categories.category_id order by products.category_id
所以我试图写一个这样的子查询:
SELECT *
FROM products, categories,
(SELECT concat(description, ' ',category) as searchable
FROM products, categories
where products.category_id=categories.category_id
group by product_id) as sub
where searchable like '%flowers%' and searchable like '%rainbow%'
and products.category_id=categories.category_id order by products.category_id;
此查询为每个 product_id 返回多个不正确的结果,当我尝试按 product_id 分组时,每个结果都显示不正确的可搜索字段。
任何帮助,将不胜感激。