gpt4 book ai didi

MySQL Left Join 没有像我预期的那样工作

转载 作者:行者123 更新时间:2023-12-01 00:08:55 24 4
gpt4 key购买 nike

表名:特征

+--------+----------+
| fea_id | fea_name |
+--------+----------+
| 1 | Price |
| 2 | Height |
| 3 | Weight |
+--------+----------+

表名:property_meta

+----+--------+--------+-------+
| id | fea_id | pro_id | value |
+----+--------+--------+-------+
| 100 | 1 | 300 | 2500 |
| 200 | 2 | 300 | 300 |
|
+----+--------+--------+-------+

我的查询

SELECT * FROM feature LEFT JOIN property_meta ON feature.fea_id = property_meta.fea_id 其中 property_meta.pro_id=300 GROUP by feature.fea_id ORDER BY feature.fea_id ASC

预期结果

+--------+--------+-------+
| fea_id | pro_id | value |
+--------+--------+-------+
| 1 | 300 | 2500 |
| 2 | 300 | 300 |
| 3 | 300 | NULL |
+--------+--------+-------+

但是我没有最后一行我也需要最后一行。我如何修改我的查询以获取最后一行?

这意味着即使属性元表中没有值,我也需要获取 Feature 表的所有行。

最佳答案

where property_meta.pro_id=300 使您的left join 成为inner join。将它添加到 on 子句中,它就可以工作了:

SELECT * FROM feature LEFT JOIN property_meta ON feature.fea_id = property_meta.fea_id and property_meta.pro_id=300 GROUP by feature.fea_id ORDER BY feature.fea_id ASC

关于MySQL Left Join 没有像我预期的那样工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44196762/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com