Mysql 查询不返回订单属性表上的空行

标签 mysql

我正在尝试执行一个查询,在该查询中检索属性以及来自不同表的值。我遇到的问题是它只返回与 order_property_values 内匹配的结果行 table 。我希望它返回所有结果,即使它在 order_property_values 中找不到匹配项。 table 。

这是我现在的查询。

SELECT * FROM `properties` as t1 
    LEFT JOIN `order_property_values` AS t2 
    ON t1.`id` =  t2.`property_id` 
    WHERE t1.`id` 
        IN (SELECT `property_id` FROM `assigned_properties` 
            WHERE `component_id` 
               IN (SELECT `component_id` FROM assigned_components 
                   WHERE `product_id` = (SELECT `product_id` 
                                         FROM `order_items` 
                                         WHERE `id` = 1)
                  )
            ) AND t2.`order_id` = 13

此查询按照我想要的方式检索所有内容,但没有空匹配行。

IN仅返回可能/可能不存在于 order_property_values 中的 ID表。

我已经尝试过NOT IN还有IS NULL但似乎无法发挥作用。

预先感谢您挽救了这一天。

最佳答案

SELECT * FROM `properties` as t1 
    LEFT JOIN `order_property_values` AS t2 
    ON t1.`id` =  t2.`property_id` AND t2.`order_id` = 13
    WHERE t1.`id` 
        IN (SELECT `property_id` FROM `assigned_properties` 
            WHERE `component_id` 
               IN (SELECT `component_id` FROM assigned_components 
                   WHERE `product_id` = (SELECT `product_id` 
                                         FROM `order_items` 
                                         WHERE `id` = 1)
                  )
            ) 

关于Mysql 查询不返回订单属性表上的空行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51196756/

相关文章:

c# - 希腊语的mysql字段可以在 Crystal 报表中显示吗?

php - Sql 查询在 php 中返回与 mysql shell 中不同的结果

php - 如何在mysql中按降序获取行?

php - 根据用户 "likes"和 "dislikes"查找最受欢迎的文章

mysql - 根据左连接数重复行 X 次

mysql - 在 SQL 中的同一字段上同时使用运算(或)与(和)(而不是不同的字段)

mysql - 尝试为 MySQL 查询创建索引

MySQL - IPv6 和 IPv4 字段类型

mysql - 如果删除了两个外键,则删除表条目

python mysql错误<class '_mysql_exceptions.ProgrammingError'>