EAV 查询和表

发布于 2024-12-28 00:18:32 字数 1077 浏览 2 评论 0原文

现在我有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

林空鹿饮溪 2025-01-04 00:18:32

这取决于您是否希望属性特定于产品,但显然您不这样做。另外,如果属性表中有product_id,则值表中不需要product_id。因此,如果您的表如下所示,则更有意义:

table categories:
-id
-category

table products:
-id
-product
-price
-image
-category_id

table attributes:
-id
-attribute
-product_id

table values:
-attribute_id
-value

实际上,我会将其变得更加简单 EAV:

table categories:
-id
-category

table products:
-id
-product
-price
-image
-category_id

table attributes:
-id
-product_id
-attribute
-value

然后,您的查询将如下所示:

SELECT id, product, price, image, attribute, value
FROM products
    INNER JOIN attributes ON products.id = attributes.product_id
WHERE products.category_id = :category_id

确保您有适当的索引。此外,您想要通过选择产品 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:

table categories:
-id
-category

table products:
-id
-product
-price
-image
-category_id

table attributes:
-id
-attribute
-product_id

table values:
-attribute_id
-value

Actually, I would make it a lot more simple EAV:

table categories:
-id
-category

table products:
-id
-product
-price
-image
-category_id

table attributes:
-id
-product_id
-attribute
-value

And then, your query would look like:

SELECT id, product, price, image, attribute, value
FROM products
    INNER JOIN attributes ON products.id = attributes.product_id
WHERE products.category_id = :category_id

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文