【问题标题】:How to aggregate different kinds of products in SQL?如何在 SQL 中聚合不同种类的产品?
【发布时间】:2016-06-08 21:46:21
【问题描述】:

假设我想销售电脑和显示器。

假设我希望计算机存储内存 (GB)、处理器速度 (GHz)、硬盘类型、硬盘大小等参数。

显示器有尺寸(英寸)、分辨率和比例等参数。

所有产品都有名称、价格和供货情况。

如果以后我想过滤它,将它们存储在 MySQL 数据库中的最佳方法是什么。 “价格范围 >500 美元的所有产品”?

【问题讨论】:

    标签: mysql


    【解决方案1】:

    使用属性值表:

    CREATE TABLE attributes (
        product_id INT, -- foreign key to products table
        attribute_name VARCHAR(32), -- size, resolution, ratio, etc.
        attribute_value VARCHAR(32) -- value of the attribute
    );
    

    然后您可以进行如下查询:

    SELECT product_id
    FROM attributes
    WHERE attribute_name = 'size' AND attribute_value = 32
    

    您可以将此表与products 表连接起来以获取价格等信息。

    如果要组合多个属性,请参见

    mySQL - Create a New Table Using Data and Columns from Three Tables

    【讨论】:

      猜你喜欢
      • 2019-07-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-21
      • 1970-01-01
      • 2010-11-24
      • 2021-05-28
      • 1970-01-01
      相关资源
      最近更新 更多