Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
凌乱的自连接Mysql选择_Mysql_Sql - Fatal编程技术网

凌乱的自连接Mysql选择

凌乱的自连接Mysql选择,mysql,sql,Mysql,Sql,我需要返回产品id的列表,这些id是 特定类别内(例如“服装”) 具有各种属性,例如“红色”或“绿色” 它们本身在属性“组”内,例如“颜色” 当我需要在多个属性组中选择多个属性选项时,我会陷入困境。例如,如果我需要返回颜色为“蓝色”或“红色”,大小为“中等”或“XXL”的产品列表 这是我的代码: SELECT `products.id` FROM `products` , `categories` , `attributes` att1, `attributes` att2 WHE

我需要返回产品id的列表,这些id是

  • 特定类别内(例如“服装”)
  • 具有各种属性,例如“红色”或“绿色”
  • 它们本身在属性“组”内,例如“颜色”
  • 当我需要在多个属性组中选择多个属性选项时,我会陷入困境。例如,如果我需要返回颜色为“蓝色”或“红色”,大小为“中等”或“XXL”的产品列表

    这是我的代码:

    SELECT `products.id` 
    FROM 
    `products` , 
    `categories` ,
    `attributes` att1, 
    `attributes` att2   
    WHERE products.id = categories.productid 
    AND `categories.id` = 3 
    AND att1.productid = products.id
    AND att1.productid = att2.productid
    AND 
    (att1.attributeid = 58 OR att1.attributeid = 60)
    AND 
    (att2.attributeid = 12 OR att2.attributeid = 9)
    

    我相信这段代码是有效的,但它看起来相当混乱,我不确定我的“脏”自连接是否正确。有人对我的问题有什么更“优雅”的解决方案吗?

    请使用现代连接语法:

    SELECT products.id
    FROM products 
    join categories on products.id = categories.productid
    join attributes att1 on att1.productid = products.id 
    join attributes att2 on att1.productid = att2.productid
    WHERE categories.id = 3 
    AND att1.attributeid IN (58, 60)
    AND att2.attributeid IN (12, 9)
    
    它更容易阅读,因为它清楚地从行过滤条件中分离连接条件。SQL优化器也更容易识别这些区别并创建更好的查询计划

    编辑 我还在(…)中添加了
    的用法。它不仅看起来更好,DB还会在
    中使用带有
    的索引,但通常不会使用
    ,即使它们的意思相同

    SELECT p.id 
    FROM   products p
    JOIN   categories  c ON  c.productid = p.id
    JOIN   attributes a1 ON a1.productid = p.id
    JOIN   attributes a2 ON a2.productid = p.id
    WHERE  categories.id = 3 
    AND    a1.attributeid IN (58, 60)
    AND    a2.attributeid IN (12,  9)
    
    我认为您犯了一个错误,将第二个属性连接到第一个属性,而不是将其连接到产品。我修好了

    再想一想,这可能是故意的,我的更正是错误的。但是,在同一个表中混合属性和属性组是一种混乱的设计


    我还简化了语法并使用了更具可读性的显式联接。

    我认为应该在(…)中使用
    而不是
    。。。或者…
    和add
    选择DISTINCT
    或者更确切地说是
    分组依据(products.id)
    (不知怎的,我感觉他只需要每种产品一次)。非常好,我会确保使用您提到的语法,看起来确实更整洁。非常感谢大家的建议@维克托不敢相信我错过了
    中的
    。更新答案。好的。