[英]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.