-4

如何在 php 和 mysql 中使用多关键字搜索?

我有一个这样的产品表

关键字字段是保存关键字ID

| id | name  | keyword_ids |
|112 | apple | 123,12,421,121|
|113 | phone | 23,14,12,1    |

和这样的关键字表

|id | name |
|1 | white |
|2 | eat   |

我想使用产品关键字字段找到相似的产品,我该怎么做?

4

1 回答 1

0

If you're set on using this data structure, you can use FIND_IN_SET

SELECT * FROM `products` p
LEFT JOIN `keyword` k ON FIND_IN_SET(k.`id`, p.`keyword_ids`)
WHERE k.`name` IN (?,?,?)

What I'd recommend doing is actually from a many-to-many relation table linking a product to keywords eg:

product_has_keyword

product_id | keyword_id
------------------------
112        | 123
112        | 12
112        | 421
112        | 121

That way you can use index for a join (which will be much faster)

于 2013-08-03T04:50:52.323 回答