简体   繁体   English

一款产品多品类 - PHP MySQL

[英]One product in many categories - PHP MySQL

I have a table products (id, barcode, catid, name, description, ...) and I've been asked to make it possible that some products can be assigned to more than one categories.我有一张表products (id、barcode、catid、name、description ......),我被要求让某些产品可以分配到多个类别。 The most obvious solution would be to change catid from INT to VARCHAR with delimited values ie 1,2,5 where 1,2 & 5 are the various categories.最明显的解决方案是将 catid 从 INT 更改为具有分隔值的 VARCHAR,即 1,2,5 其中 1,2 和 5 是各种类别。

Which SQL statement will do the job for me now?哪个 SQL 语句现在可以为我完成这项工作? I have find_in_set in mind but I think it does the opposite.我有 find_in_set 的想法,但我认为它恰恰相反。

The most obvious solution would be to create a 'cross-table' where you can do a many-to-many assignment (as in one category can have many products and one product can have many categories).最明显的解决方案是创建一个“交叉表”,您可以在其中进行多对多分配(因为一个类别可以有多个产品,一个产品可以有多个类别)。 This table would look like this:该表如下所示:

product_id | category_id

You can then use this query:然后,您可以使用此查询:

SELECT 
  *
FROM 
  (product JOIN product_has_category ON product.id = product_has_category.product_id) 
      JOIN category ON product_has_category.category_id = category.id

You can even use normal WHERE and `ORDER BY, etc.你甚至可以使用普通的WHERE和 `ORDER BY 等。

edit: Fixed a major error in my query, my appologies!编辑:修复了我的查询中的一个重大错误,我的道歉!


If you are unable to change the database, you might want to try this:如果您无法更改数据库,您可能需要尝试以下操作:

SELECT 
  *
FROM 
  category, product
WHERE
  product.id = ?? AND FIND_IN_SET(category.id, product.catid) > 0 GROUP BY product.id

Warning: It is a long shot, I haven't tested this, but as FIND_IN_SET would search a comma-seperated list, it could work.警告:这是一个很长的镜头,我还没有测试过,但是由于 FIND_IN_SET 会搜索一个逗号分隔的列表,它可以工作。

Don't delimit your category ideas in a single field.不要将您的类别创意划定在一个领域。 You're setting yourself up for much pain.你让自己承受很大的痛苦。 Use a many to many relationship.使用多对多关系。

products:
id,
barcode
...

category:
cat_id,
cat_name

product_category:
product_id
cat_id

To get all products in a particular category:要获取特定类别中的所有产品:

SELECT * FROM products JOIN product_category ON product_id=id WHERE cat_id=?

To get all categories for a product获取产品的所有类别

SELECT * FROM category JOIN product_category ON product_category.cat_id = category.cat_id WHERE product_id=?

Your best bet would be to have a separate table with 'categories' and entries for each product.您最好的选择是有一个单独的表格,其中包含每个产品的“类别”和条目。 Normalize it this way.以这种方式对其进行标准化。

If you don't want to do that, use a bitmask.如果您不想这样做,请使用位掩码。 Its much easier than parsing a comma delimited list of entries.它比解析逗号分隔的条目列表要容易得多。

drop the catid and use a table that associates the id to one catid per row (cross-table).删除 catid 并使用将 id 关联到每行一个 catid 的表(交叉表)。

I'd suggest the better solution would be to have a second table that maps product ids to categories, with an entry for each category that matches a product.我建议更好的解决方案是使用第二个表将产品 ID 映射到类别,每个类别都有一个与产品匹配的条目。 It makes querying categories a little more complex by having to use JOIN, but is a more relational-database way than having a field with delimited values within the same table, which sounds like a nightmare to write queries for.由于必须使用 JOIN,它使得查询类别稍微复杂一些,但它是一种比在同一个表中具有带分隔值的字段更关系数据库的方式,这听起来像是编写查询的噩梦。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM