简体   繁体   English

数据库设计:存储带有多个选择的多个选项的最佳方法

[英]database design: best way to store multiple options with multiple selects

So i am trying to determine what is the best way to do this: I will try to make this as simple as possible but give enough info 因此,我正在尝试确定执行此操作的最佳方法是什么:我将尝试使此操作尽可能简单,但要提供足够的信息

I have a database with a large dataset. 我有一个带有大型数据集的数据库。 for this example each car is a car that someone is looking for 在此示例中,每辆汽车都是某人正在寻找的汽车

i have a table: 我有一张桌子:

CREATE TABLE cars (
`car_id` INT(10),
`car_name` VARCHAR(20)
)  

and options table like 和选项表像

CREATE TABLE `car_selected_options` (
`car_sel_id`   INT       NOT NULL AUTO_INCREMENT,
`car_id`       INT       //the id of the car record created
`value`        INT       //id of info in a ref table
`key`          VARCHAR(20)   //from the car_option_reference,
)

some examples would be: the value is the id of more info in a ref table(note two electronics) 一些示例将是:该值是参考表中更多信息的ID(请注意两个电子产品)

car_id=1, value='10',    key='exterior_color'  10='red'
car_id=1, value='21',    key='interior_color'  21='blue' 
car_id=1, value='100',   key='electronics'     100='radio'
car_id=1, value='101',   key='electronics'     101='nav'

I need to find all the car id's that are red and have nav and radios i am doing something like: 我需要找到所有红色的,具有导航和收音机功能的汽车ID,我正在执行以下操作:

SELECT distinct(c.car_id)
FROM `car` c 
   INNER JOIN `car_selected_options` AS o ON c.car_id = o.car_id
WHERE 
o.car_sel_id IN 
        ( SELECT car_sel_id 
            FROM car_selected_options so 
            WHERE so.`key` = 'exterior_color' 
              AND so.value IN ('10' ) AND b.buyer_id = bm.buyer_id )
    AND
o.car_sel_id IN 
        ( SELECT car_sel_id 
            FROM car_selected_options so 
            WHERE so.`key` = 'electronics'  
              AND so.value IN ('100','101' ) AND b.buyer_id = bm.buyer_id )

or would it be better to instead of having the keys and everthing in one table have each key.option with separate tables for each thing like: 还是最好不要在每个表中都包含键和所有东西,而将每个key.option的每个表都带有单独的表,例如:

car_exterior_color (could have one entry per car or multiples if this person is ok with a red or a blue colored car)
car_interior_color 
car_electronics

and instead of doing these multiple sub selects (could be many) , do multiple inner joins? 而不是执行这些多个子选择(可能很多),而是执行多个内部联接?

I hope this makes sense thanks for any help 我希望这很有意义,谢谢您的帮助

There seems to be something missing from the query you posted because I don't see what the "b" and "bm" tables are. 您发布的查询中似乎缺少某些内容,因为我看不到“ b”和“ bm”表是什么。 Ignoring that clause, you can greatly simplify your query without doing multiple subselects: 忽略该子句,您可以大大简化查询,而无需执行多个子选择:

SELECT c.car_id
FROM `car` c 
   INNER JOIN `car_selected_options` AS o ON c.car_id = o.car_id
WHERE 
   (o.key = 'exterior_color' AND o.value = 10) OR
   (o.key = 'electronics' AND o.value = 100) OR
   (o.key = 'electronics' AND o.value = 101) OR
GROUP BY c.car_id
HAVING COUNT(*) = 3;

The "3" in this case is the number of criteria you are applying. 在这种情况下,“ 3”是您要应用的条件数。 The trick here is that for each matching car you should have exactly three matching rows, so if you group by car_id, and filter by count(*) = 3 you will select only the cars that match on ALL criteria. 这里的窍门是,对于每辆匹配的汽车,您应该恰好有三排匹配的行,因此,如果您按car_id分组,并按count(*)= 3进行过滤,则只会选择符合所有条件的汽车。

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

相关问题 设计具有多个变体的产品的数据库的最佳方法是什么? - Whats the best way to design a database with a product of multiple variations? 数据库设计-存储模板和实例的最佳方法 - Database design - best way to store templates and instances 这是从具有多个选择的下拉菜单中存储选项的方法吗? - Is this the way to go for storing options from a dropdown with multiple selects? 在数据库中存储多个复选框值并在以后使用它的最佳方法是什么 - What is the best way to store multiple checkbox values in the database and work with it later on 在mysql数据库中存储多个用户数据的最佳方法是什么? - What is the best way to store multiple user data in mysql database? 在多列中存储多个 ID 的最佳方式 - Best way to store multiple ids in multiple columns MySQL:将多个MAX()选择与数组组合的最佳方法? - MySQL: Best way to combine multiple MAX() selects with an array? 连接多个表的数据库设计最佳实践 - database design best practice connecting multiple tables 在MySQL数据库中存储10个选项的最佳方法是什么? - What is the best way to store 10 options in a mysql database? 数据库设计:存储父类的异构子类型的最佳方法? - Database design: Best Way to store heterogeneous subtypes of a parent class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM