select * from order
-------------------
|orderID|productID|
-------------------
| 1 | 234 |
| 2 | 234 |
| 3 | 123 |
-------------------
select * from product_supplier
-------------------------------------------
|ID|supplierID|productID|cost_price|latest|
-------------------------------------------
|1 | 1 | 234 | 1.00 | 0 |
|2 | 1 | 234 | 0.50 | 1 |
|3 | 2 | 123 | 0.75 | 1 |
-------------------------------------------
desired result
------------------------------
|orderID|productID|cost_price|
------------------------------
| 1 | 234 | 1.00 |
| 2 | 234 | 1.00 |
| 3 | 123 | 0.75 |
------------------------------
我正在查看上面的两个表格,以获得给定orderID
的{{1}},productID
和最大cost_price
。
productID
给出SELECT orderID, productID, cost_price
FROM order LEFT JOIN product_supplier
ON order.productID=product_supplier.productID AND MAX(cost_price);
如何将ERROR 1111 (HY000): Invalid use of group function
ed表限制为每join
行一行,加上order
中对应的最大cost_price
值?
答案 0 :(得分:3)
接近此方法的规范方法是使用子查询从product_supplier
表中标识产品及其最高价格,然后将此子查询加入order
以获取所需的结果集
SELECT t1.orderID,
t1.productID,
COALESCE(t2.cost_price, 0.0) AS cost_price -- missing products will appear
FROM order t1 -- with a zero price
LEFT JOIN
(
SELECT productID, MAX(cost_price) AS cost_price
FROM product_supplier
GROUP BY productID
) t2
ON t1.productID = t2.productID AND
t1.cost_price = t2.cost_price
答案 1 :(得分:0)
当您需要使用聚合时,您必须使用group by和compare condtion使用拥有
new Promise((resolve, reject) => {
const a = 1;
resolve(Promise.all([
Promise.resolve("a"),
Promise.resolve("b"),
a,
]))
})
.then(([resultFromPromise_1, resultFromPromise_2, a]) => {
console.log(a);
})