MySQL +查询返回所有在另一个表中具有“活动”列的列的所有行

时间:2018-08-11 23:59:10

标签: mysql

我正在寻找有关如何进行此查询的帮助..以及如何高效地进行查询。

我对MySQL /查询不是(不是)(我的技能在JOIN级别上变得混浊!..大声笑)。

因此,我也会牺牲我实际上理解的可读取代码/查询的效率。 :)

我还有一个Rextester设置示例供您查看:

http://rextester.com/FWVO47690

  • 我知道当前有很多列..(有些会消失)..关键列是与饮料和成分表相匹配的Ingredient_code。

  • 对于酒水桌..我可以像在桌子上勾勒出轮廓一样...

或者我只可以有一个成分栏...但是数据如下:

将从饮料菜单界面中发送哪些动作(序列)数据。.如果这样可以更轻松地使用,而不是将每种成分,位置和数量分成可能的15个单独的列呢?

摘要: 我正在创建一个前端界面(网页),该界面托管在安装了LAMP(或多或少)的Raspberry Pi上。

这是饮料菜单。= “饮料桌”或多或少是所有饮料的“图书馆” ...

加载“页面”时..我想运行/执行查询以返回饮料表中的所有饮料..在另一个表中具有“匹配和有效”成分。 (希望这样吗?)

所以根据成分表,饮料菜单有点“动态”。

如果需要喝酒:

橙汁和伏特加酒..

如果在配料表中橙汁或伏特加酒未激活,则该菜单将不会激活或在饮料菜单中显示。等。

**每种饮料可以包含1到15种成分...

所以我不确定如何通过查询执行此操作?我确定是否需要一些联接和子查询...

所以我正在寻找一些指导(至少关于要搜索的关键词..etc)

我觉得这不是很有效吗? (虽然我不必太担心“速度”,因为它在每个电源循环时只需要加载一次。...我不希望它落后或出现任何问题..大声笑)

我尝试了这种尝试。.但是这不允许在饮料中添加多种成分。.我只做了“ 1”。.

SELECT * FROM barbot_drinks AS drinks
WHERE EXISTS (
  SELECT * FROM barbot_ingredients AS ingredients 
  WHERE drinks.ingredient_1_code = ingredients.ingredient_code
)

像这样的东西会带来误报..因为其中一种成分可能会丢失..但是前一种或后一种成分会“匹配” ..因此它会被退回(不应该使用时)

SELECT * FROM barbot_drinks AS drinks
WHERE EXISTS(
  SELECT * FROM barbot_ingredients AS ingredients    
  WHERE drinks.ingredient_1_code = ingredients.ingredient_code 
    OR drinks.ingredient_2_code = ingredients.ingredient_code 
    OR drinks.ingredient_3_code = ingredients.ingredient_code 
    OR drinks.ingredient_4_code = ingredients.ingredient_code 
    OR drinks.ingredient_5_code = ingredients.ingredient_code 
    OR drinks.ingredient_6_code = ingredients.ingredient_code 
    OR drinks.ingredient_7_code = ingredients.ingredient_code 
    OR drinks.ingredient_8_code = ingredients.ingredient_code 
    OR drinks.ingredient_9_code = ingredients.ingredient_code 
    OR drinks.ingredient_10_code = ingredients.ingredient_code 
    OR drinks.ingredient_11_code = ingredients.ingredient_code 
    OR drinks.ingredient_12_code = ingredients.ingredient_code 
    OR drinks.ingredient_13_code = ingredients.ingredient_code 
    OR drinks.ingredient_14_code = ingredients.ingredient_code 
    OR drinks.ingredient_15_code = ingredients.ingredient_code 

);

有关尼克斯提供的解决方案的更新:

**之所以要进行更新,是因为如果您更改表,则通过删除(比如说)Ingredient_x_dispenser值(或_code或_dosage)..但保留其他值仍然返回。.

对于每个饮料行,每个成分都必须具有所有3个字段(_dispenser,_code,_dosage)!=''... &&在另一张表中也具有该component_code并处于活动状态) ..对不起,如果我没有正确传达此信息。

@尼克(这样吗?)

SELECT * 
FROM barbot_drinks d
WHERE (ingredient_1_dispenser = '' AND ingredient_1_code = '' AND ingredient_1_dosage = '' OR 
    EXISTS(SELECT * FROM barbot_ingredients i WHERE i.active = 1 AND i.ingredient_code = d.ingredient_1_code)) AND
    (ingredient_2_dispenser = '' AND ingredient_2_code = '' AND ingredient_2_dosage = '' OR
    EXISTS(SELECT * FROM barbot_ingredients i WHERE i.active = 1 AND i.ingredient_code = d.ingredient_2_code)) AND
    (ingredient_3_dispenser = '' AND ingredient_3_code = '' AND ingredient_3_dosage = '' OR
    EXISTS(SELECT * FROM barbot_ingredients i WHERE i.active = 1 AND i.ingredient_code = d.ingredient_3_code)) AND
    (ingredient_4_dispenser = '' AND ingredient_4_code = '' AND ingredient_4_dosage = '' OR
    EXISTS(SELECT * FROM barbot_ingredients i WHERE i.active = 1 AND i.ingredient_code = d.ingredient_4_code)) AND
    (ingredient_5_dispenser = '' AND ingredient_5_code = '' AND ingredient_5_dosage = '' OR
    EXISTS(SELECT * FROM barbot_ingredients i WHERE i.active = 1 AND i.ingredient_code = d.ingredient_5_code)) AND
    (ingredient_6_dispenser = '' AND ingredient_6_code = '' AND ingredient_6_dosage = '' OR
    EXISTS(SELECT * FROM barbot_ingredients i WHERE i.active = 1 AND i.ingredient_code = d.ingredient_6_code)) AND
    (ingredient_7_dispenser = '' AND ingredient_7_code = '' AND ingredient_7_dosage = '' OR
    EXISTS(SELECT * FROM barbot_ingredients i WHERE i.active = 1 AND i.ingredient_code = d.ingredient_7_code)) AND
    (ingredient_8_dispenser = '' AND ingredient_8_code = '' AND ingredient_8_dosage = '' OR
    EXISTS(SELECT * FROM barbot_ingredients i WHERE i.active = 1 AND i.ingredient_code = d.ingredient_8_code)) AND
    (ingredient_9_dispenser = '' AND ingredient_9_code = '' AND ingredient_9_dosage = '' OR
    EXISTS(SELECT * FROM barbot_ingredients i WHERE i.active = 1 AND i.ingredient_code = d.ingredient_9_code)) AND
    (ingredient_10_dispenser = '' AND ingredient_10_code = '' AND ingredient_10_dosage = '' OR
    EXISTS(SELECT * FROM barbot_ingredients i WHERE i.active = 1 AND i.ingredient_code = d.ingredient_10_code)) AND
    (ingredient_11_dispenser = '' AND ingredient_11_code = '' AND ingredient_11_dosage = '' OR
    EXISTS(SELECT * FROM barbot_ingredients i WHERE i.active = 1 AND i.ingredient_code = d.ingredient_11_code)) AND
    (ingredient_12_dispenser = '' AND ingredient_12_code = '' AND ingredient_12_dosage = '' OR
    EXISTS(SELECT * FROM barbot_ingredients i WHERE i.active = 1 AND i.ingredient_code = d.ingredient_12_code)) AND
    (ingredient_13_dispenser = '' AND ingredient_13_code = '' AND ingredient_13_dosage = '' OR
    EXISTS(SELECT * FROM barbot_ingredients i WHERE i.active = 1 AND i.ingredient_code = d.ingredient_13_code)) AND
    (ingredient_14_dispenser = '' AND ingredient_14_code = '' AND ingredient_14_dosage = '' OR
    EXISTS(SELECT * FROM barbot_ingredients i WHERE i.active = 1 AND i.ingredient_code = d.ingredient_14_code)) AND
    (ingredient_15_dispenser = '' AND ingredient_15_code = '' AND ingredient_15_dosage = '' OR
    EXISTS(SELECT * FROM barbot_ingredients i WHERE i.active = 1 AND i.ingredient_code = d.ingredient_15_code));

我觉得应该更像这样:(但这返回零行)

SELECT * 
FROM barbot_drinks d
WHERE (ingredient_1_dispenser != '' AND ingredient_1_code != '' AND ingredient_1_dosage != '' AND 
    EXISTS(SELECT * FROM barbot_ingredients i WHERE i.active = 1 AND i.ingredient_code = d.ingredient_1_code)) AND
    (ingredient_2_dispenser != '' AND ingredient_2_code != '' AND ingredient_2_dosage != '' AND
    EXISTS(SELECT * FROM barbot_ingredients i WHERE i.active = 1 AND i.ingredient_code = d.ingredient_2_code)) AND
    (ingredient_3_dispenser != '' AND ingredient_3_code != '' AND ingredient_3_dosage != '' AND
    EXISTS(SELECT * FROM barbot_ingredients i WHERE i.active = 1 AND i.ingredient_code = d.ingredient_3_code)) AND
    (ingredient_4_dispenser != '' AND ingredient_4_code != '' AND ingredient_4_dosage != '' AND
    EXISTS(SELECT * FROM barbot_ingredients i WHERE i.active = 1 AND i.ingredient_code = d.ingredient_4_code)) AND
    (ingredient_5_dispenser != '' AND ingredient_5_code != '' AND ingredient_5_dosage != '' AND
    EXISTS(SELECT * FROM barbot_ingredients i WHERE i.active = 1 AND i.ingredient_code = d.ingredient_5_code)) AND
    (ingredient_6_dispenser != '' AND ingredient_6_code != '' AND ingredient_6_dosage != '' AND
    EXISTS(SELECT * FROM barbot_ingredients i WHERE i.active = 1 AND i.ingredient_code = d.ingredient_6_code)) AND
    (ingredient_7_dispenser != '' AND ingredient_7_code != '' AND ingredient_7_dosage != '' AND
    EXISTS(SELECT * FROM barbot_ingredients i WHERE i.active = 1 AND i.ingredient_code = d.ingredient_7_code)) AND
    (ingredient_8_dispenser != '' AND ingredient_8_code != '' AND ingredient_8_dosage != '' AND
    EXISTS(SELECT * FROM barbot_ingredients i WHERE i.active = 1 AND i.ingredient_code = d.ingredient_8_code)) AND
    (ingredient_9_dispenser != '' AND ingredient_9_code != '' AND ingredient_9_dosage != '' AND
    EXISTS(SELECT * FROM barbot_ingredients i WHERE i.active = 1 AND i.ingredient_code = d.ingredient_9_code)) AND
    (ingredient_10_dispenser != '' AND ingredient_10_code != '' AND ingredient_10_dosage != '' AND
    EXISTS(SELECT * FROM barbot_ingredients i WHERE i.active = 1 AND i.ingredient_code = d.ingredient_10_code)) AND
    (ingredient_11_dispenser != '' AND ingredient_11_code != '' AND ingredient_11_dosage != '' AND
    EXISTS(SELECT * FROM barbot_ingredients i WHERE i.active = 1 AND i.ingredient_code = d.ingredient_11_code)) AND
    (ingredient_12_dispenser != '' AND ingredient_12_code != '' AND ingredient_12_dosage != '' AND
    EXISTS(SELECT * FROM barbot_ingredients i WHERE i.active = 1 AND i.ingredient_code = d.ingredient_12_code)) AND
    (ingredient_13_dispenser != '' AND ingredient_13_code != '' AND ingredient_13_dosage != '' AND
    EXISTS(SELECT * FROM barbot_ingredients i WHERE i.active = 1 AND i.ingredient_code = d.ingredient_13_code)) AND
    (ingredient_14_dispenser != '' AND ingredient_14_code != '' AND ingredient_14_dosage != '' AND
    EXISTS(SELECT * FROM barbot_ingredients i WHERE i.active = 1 AND i.ingredient_code = d.ingredient_14_code)) AND
    (ingredient_15_dispenser != '' AND ingredient_15_code != '' AND ingredient_15_dosage != '' AND
    EXISTS(SELECT * FROM barbot_ingredients i WHERE i.active = 1 AND i.ingredient_code = d.ingredient_15_code));

重述:

我只想从饮料表中退回饮料:

  • 如果Ingredient_x_dispenser中存在一个值,则-or- Ingredient_x_code,-or- Ingredient_x_dosage ..存在所有 x 的'3'值。。(_ dispenser,_code,_dosage为了讨论的目的,在我的脑海中补上1个“成分”,每个配方(行)可以有1-15个“成分”,希望这是有道理的。)

  • 如果在饮料行中满足了-3-成分要求,则还必须在其他成分表中找到“ ACTIVE”并具有匹配的_CODE(如现在一样)。它遵循的标准的第一部分

1 个答案:

答案 0 :(得分:1)

首先,我要说您确实需要规范化数据库。但这是另一个问题。同时,您可以使用此查询。我只显示了四种成分,您需要根据情况将其扩展到15种。

SELECT * 
FROM barbot_drinks d
WHERE (ingredient_1_dispenser = '' OR 
       EXISTS(SELECT * FROM barbot_ingredients i WHERE i.active = 1 AND i.dispenser_type = d.ingredient_1_dispenser AND i.ingredient_code = d.ingredient_1_code)) AND
      (ingredient_2_dispenser = '' OR
       EXISTS(SELECT * FROM barbot_ingredients i WHERE i.active = 1 AND i.dispenser_type = d.ingredient_2_dispenser AND i.ingredient_code = d.ingredient_2_code)) AND
      (ingredient_3_dispenser = '' OR
       EXISTS(SELECT * FROM barbot_ingredients i WHERE i.active = 1 AND i.dispenser_type = d.ingredient_3_dispenser AND i.ingredient_code = d.ingredient_3_code)) AND
      (ingredient_4_dispenser = '' OR
       EXISTS(SELECT * FROM barbot_ingredients i WHERE i.active = 1 AND i.dispenser_type = d.ingredient_4_dispenser AND i.ingredient_code = d.ingredient_4_code))

使用您的样本数据,将产生以下输出:

id  drink_id    drink_name   drink_image      drink_desc                    ingredient_1_dispenser  ingredient_1_code   ingredient_1_dosage     ingredient_2_dispenser  ingredient_2_code   ingredient_2_dosage     ingredient_3_dispenser  ingredient_3_code   ingredient_3_dosage     ingredient_4_dispenser  ingredient_4_code   ingredient_4_dosage
1   vdk_org     Screw Driver screw_driver.jpg Screw Driver description...   bottle  vdk     1   valve   oj  2000                                                                                                                                                            
2   vdk_cran    Cape Cod     cape_cod.jpg     Cape Cod description...       bottle  vdk     1   valve   cbj     2000                                                                                                                                                            
3   dry_mrtn    Dry Martini  dry_martini.jpg  Dry Martini description...    bottle  vdk     2.5     bottle  vrmth   .5  valve   orgbit  200

修改

基于与OP的一些扩展讨论,对该查询进行了如下改进。此查询要求饮料中每种成分都包含分配器,代码和剂量。 SQLFiddle可用。

SELECT * 
FROM barbot_drinks d
WHERE (ingredient_1_dispenser = '' AND ingredient_1_code = '' AND ingredient_1_dosage = '' OR
       ingredient_1_dosage != '' AND EXISTS(SELECT * FROM barbot_ingredients i WHERE i.active = 1 AND i.dispenser_type = d.ingredient_1_dispenser AND i.ingredient_code = d.ingredient_1_code)) AND
      (ingredient_2_dispenser = '' AND ingredient_2_code = '' AND ingredient_2_dosage = '' OR
       ingredient_2_dosage != '' AND EXISTS(SELECT * FROM barbot_ingredients i WHERE i.active = 1 AND i.dispenser_type = d.ingredient_2_dispenser AND i.ingredient_code = d.ingredient_2_code)) AND
      (ingredient_3_dispenser = '' AND ingredient_3_code = '' AND ingredient_3_dosage = '' OR
       ingredient_3_dosage != '' AND EXISTS(SELECT * FROM barbot_ingredients i WHERE i.active = 1 AND i.dispenser_type = d.ingredient_3_dispenser AND i.ingredient_code = d.ingredient_3_code)) AND
      (ingredient_4_dispenser = '' AND ingredient_4_code = '' AND ingredient_4_dosage = '' OR
       ingredient_4_dosage != '' AND EXISTS(SELECT * FROM barbot_ingredients i WHERE i.active = 1 AND i.dispenser_type = d.ingredient_4_dispenser AND i.ingredient_code = d.ingredient_4_code)) AND
      (ingredient_5_dispenser = '' AND ingredient_5_code = '' AND ingredient_5_dosage = '' OR
       ingredient_5_dosage != '' AND EXISTS(SELECT * FROM barbot_ingredients i WHERE i.active = 1 AND i.dispenser_type = d.ingredient_5_dispenser AND i.ingredient_code = d.ingredient_5_code)) AND
      (ingredient_6_dispenser = '' AND ingredient_6_code = '' AND ingredient_6_dosage = '' OR
       ingredient_6_dosage != '' AND EXISTS(SELECT * FROM barbot_ingredients i WHERE i.active = 1 AND i.dispenser_type = d.ingredient_6_dispenser AND i.ingredient_code = d.ingredient_6_code)) AND
      (ingredient_7_dispenser = '' AND ingredient_7_code = '' AND ingredient_7_dosage = '' OR
       ingredient_7_dosage != '' AND EXISTS(SELECT * FROM barbot_ingredients i WHERE i.active = 1 AND i.dispenser_type = d.ingredient_7_dispenser AND i.ingredient_code = d.ingredient_7_code)) AND
      (ingredient_8_dispenser = '' AND ingredient_8_code = '' AND ingredient_8_dosage = '' OR
       ingredient_8_dosage != '' AND EXISTS(SELECT * FROM barbot_ingredients i WHERE i.active = 1 AND i.dispenser_type = d.ingredient_8_dispenser AND i.ingredient_code = d.ingredient_8_code)) AND
      (ingredient_9_dispenser = '' AND ingredient_9_code = '' AND ingredient_9_dosage = '' OR
       ingredient_9_dosage != '' AND EXISTS(SELECT * FROM barbot_ingredients i WHERE i.active = 1 AND i.dispenser_type = d.ingredient_9_dispenser AND i.ingredient_code = d.ingredient_9_code)) AND
      (ingredient_10_dispenser = '' AND ingredient_10_code = '' AND ingredient_10_dosage = '' OR
       ingredient_10_dosage != '' AND EXISTS(SELECT * FROM barbot_ingredients i WHERE i.active = 1 AND i.dispenser_type = d.ingredient_10_dispenser AND i.ingredient_code = d.ingredient_10_code)) AND
      (ingredient_11_dispenser = '' AND ingredient_11_code = '' AND ingredient_11_dosage = '' OR
       ingredient_11_dosage != '' AND EXISTS(SELECT * FROM barbot_ingredients i WHERE i.active = 1 AND i.dispenser_type = d.ingredient_11_dispenser AND i.ingredient_code = d.ingredient_11_code)) AND
      (ingredient_12_dispenser = '' AND ingredient_12_code = '' AND ingredient_12_dosage = '' OR
       ingredient_12_dosage != '' AND EXISTS(SELECT * FROM barbot_ingredients i WHERE i.active = 1 AND i.dispenser_type = d.ingredient_12_dispenser AND i.ingredient_code = d.ingredient_12_code)) AND
      (ingredient_13_dispenser = '' AND ingredient_13_code = '' AND ingredient_13_dosage = '' OR
       ingredient_13_dosage != '' AND EXISTS(SELECT * FROM barbot_ingredients i WHERE i.active = 1 AND i.dispenser_type = d.ingredient_13_dispenser AND i.ingredient_code = d.ingredient_13_code)) AND
      (ingredient_14_dispenser = '' AND ingredient_14_code = '' AND ingredient_14_dosage = '' OR
       ingredient_14_dosage != '' AND EXISTS(SELECT * FROM barbot_ingredients i WHERE i.active = 1 AND i.dispenser_type = d.ingredient_14_dispenser AND i.ingredient_code = d.ingredient_14_code)) AND
      (ingredient_15_dispenser = '' AND ingredient_15_code = '' AND ingredient_15_dosage = '' OR
       ingredient_15_dosage != '' AND EXISTS(SELECT * FROM barbot_ingredients i WHERE i.active = 1 AND i.dispenser_type = d.ingredient_15_dispenser AND i.ingredient_code = d.ingredient_15_code))