【发布时间】:2018-06-15 18:41:22
【问题描述】:
我有两张桌子:products 和 prices
products
- id (PK)
- 姓名
prices
- id (PK)
- product_id(FK > 产品)
- 价格
- 原价
每个产品可能有多个价格。我想要实现的是一个返回我all products on-sale with its cheapest price的查询。
-
on-sale= 价格 - 如果产品不是
on-sale,则不应包含在结果中 - 如果产品有多个符合
on-sale条件的价格,则只返回最便宜的价格。
结果表应该有这些列
- products.id
- products.name
- prices.id
- prices.price
- prices.originalPrice
通过我的尝试,我最终解决了这个问题:#1055 - Expression #3 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'tbl.price' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by。 请注意,我无法更改配置。
MySQL 版本: 5.7.22
我在此处上传了包含示例数据的 SQL 导出: https://www.dropbox.com/s/6ucdv6592dum6n6/stackoverflow_export.sql?dl=0
【问题讨论】:
-
您能否为 TABLE 架构提供数据?
-
哪一栏标记
on-sale或不on-sale -
我在这里上传了一个带有示例数据的 SQL 导出:dropbox.com/s/6ucdv6592dum6n6/stackoverflow_export.sql?dl=0
-
下面添加的答案你可以检查吗?
标签: mysql join select distinct