EAV 查询和表
现在我有 4 个表:
table categories:
-id
-category
table products:
-id
-product
-price
-image
table attributes:
-id
-attribute
-product_id
table values:
-product_id
-attribute_id
-value
我正在查询:
SELECT `id`, `product`, `price`, `image` FROM `products` WHERE `category_id` = $category->id
现在我得到了该类别的一系列产品,并且需要获取它的属性: 下一个查询:
SELECT `products`.`id` AS `product_id`,
`attributes`.`attribute`,
`values`.`value`
FROM `products` LEFT JOIN `attributes` ON (`attributes`.`product_id` = `products`.`id`)
LEFT JOIN `values` ON (`values`.`product_id` = `products`.`id`
AND `values`.`attribute_id` = `attributes`.`id`)
WHERE `products`.`id` IN ($ids)
它获取了带有值的属性,但我想知道一件事: 是否可以删除表属性
中的'product_id'
列并获取没有该列的属性和值?现在它是一大堆重复的属性,例如:
table attributes
-id 1
-attribute Weight
-product_id 1
-id 2
-attribute Weight
-product_id 2
虽然我只想:
-id 1
-attribute Weight
对不起我的英语,如果我的帖子的某些部分需要更多解释,请现在就告诉我
Now I have 4 tables:
table categories:
-id
-category
table products:
-id
-product
-price
-image
table attributes:
-id
-attribute
-product_id
table values:
-product_id
-attribute_id
-value
And I'm querying with:
SELECT `id`, `product`, `price`, `image` FROM `products` WHERE `category_id` = $category->id
Now I got array of products for this category and need to get it's properties:
next query:
SELECT `products`.`id` AS `product_id`,
`attributes`.`attribute`,
`values`.`value`
FROM `products` LEFT JOIN `attributes` ON (`attributes`.`product_id` = `products`.`id`)
LEFT JOIN `values` ON (`values`.`product_id` = `products`.`id`
AND `values`.`attribute_id` = `attributes`.`id`)
WHERE `products`.`id` IN ($ids)
And it's get the attributes with values but I'm wondering about one thing:
If it's possible to get rid of 'product_id'
column in table attributes
and get attributes and values without that column? Now it's a whole bunch of duplicating attributes for example:
table attributes
-id 1
-attribute Weight
-product_id 1
-id 2
-attribute Weight
-product_id 2
While I want just:
-id 1
-attribute Weight
sorry for my english, if some part of my post needs more explanation please let me now
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这取决于您是否希望属性特定于产品,但显然您不这样做。另外,如果属性表中有product_id,则值表中不需要product_id。因此,如果您的表如下所示,则更有意义:
实际上,我会将其变得更加简单 EAV:
然后,您的查询将如下所示:
确保您有适当的索引。此外,您想要通过选择产品 ID 然后将它们放入 IN 中的方式也是一种不好的做法。
It depends if you want your attributes to be product specific, but you obviously, don't. Also, you don't need product_id in your values table, if you have it in your attributes table. So your tables make more sense if they are like:
Actually, I would make it a lot more simple EAV:
And then, your query would look like:
Make sure you have appropriate indexes. Also, the way you wanted to do it with selecting product ids and then putting them into IN, is a bad practice.