从mySQL中的两个表中进行选择,带有缺失的通讯器
Consider the following example:You have two tables, categories (with columns id and title) and products (with columns id, ownerid, title, and price, where ownerid references id in the categories table).
Your query aims to retrieve all categories and their minimum and maximum价格,按类别ID分组。但是,当前查询不包括没有相应产品的类别。
要解决此问题,请使用左JOIN:的组左连接确保包括类别表中的所有行,即使产品表中没有相应的行。 IFNULL函数将0分配给MINP和MAXP,用于没有产品的类别,以防止显示null值。
另外,您可能更喜欢返回这些类别的NULL,而不是0,取决于您的特定要求。免责声明: 提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发到邮箱:[email protected] 我们会第一时间内为您处理。
Copyright© 2022 湘ICP备2022001581号-3