子查询并为MySQL中的多个外键选择最早的行

子查询并为MySQL中的多个外键选择最早的行,sql,mysql,subquery,Sql,Mysql,Subquery,我有两张桌子: product (idproduct, name, description, tax) product_storage (idstorage, idproduct, added, quantity, price) 对于每种产品,价格可能不同,最老的产品首先销售 例如,在存储器中,我有: 1, 1, 2010-01-01, 0, 10.0 2, 1, 2010-01-02, 0, 11.0 3, 1, 2010-01-03, 10, 12.0 4, 2, 2010-01-0

我有两张桌子:

product (idproduct, name, description, tax)

product_storage (idstorage, idproduct, added, quantity, price)
对于每种产品,价格可能不同,最老的产品首先销售

例如,在存储器中,我有:

1, 1, 2010-01-01,  0, 10.0
2, 1, 2010-01-02,  0, 11.0
3, 1, 2010-01-03, 10, 12.0
4, 2, 2010-01-04,  0, 12.0
5, 2, 2010-01-05, 10, 11.0
6, 2, 2010-01-06, 10, 13.0
7, 3, 2010-01-07, 10, 14.0
8, 3, 2010-01-08, 10, 16.0
9, 3, 2010-01-09, 10, 13.0
现在我需要选择当前价格的所有产品,这是product_storage中最早的一行,其中每个产品的数量>0:

SELECT p.idproduct, p.name, p.tax,
       (SELECT s.price
        FROM product_storage s
        WHERE s.idproduct=p.idproduct AND s.quantity > 0
        ORDER BY s.added ASC
        LIMIT 1) AS price
FROM product p;
工作正常,但当我想在查询中计算含税价格时,它不起作用:

SELECT p.idproduct, p.name, p.tax,
       (SELECT s.price
        FROM product_storage s
        WHERE s.idproduct=p.idproduct AND s.quantity > 0
        ORDER BY s.added ASC
        LIMIT 1) AS price,
        (price * (1 + tax/100)) AS price_with_tax
FROM product p;
MySQL说:

Unknown column 'price' in 'field list'
更新

使用子查询作为一个表几乎可以解决问题看看答案-现在唯一的问题是如何从product_存储中为多个外键选择最旧的行,并且每个idproduct只能选择一个

更新2


感谢cmptrgeekken提供了出色的解决方案:

这几乎可以正常工作:

SELECT p.idproduct, p.name, p.tax,
       sub.price, (sub.price * (1+tax/100)) as price_with_tax
FROM product p,
    (SELECT s.idproduct, s.price
     FROM product_storage s
     WHERE quantity > 0
     ORDER BY added ASC) sub
WHERE p.idproduct=sub.idproduct

但是product_存储中的所有行都会针对每个产品返回://

就MySQL而言,最后出现的单词price是指product p中的一个字段,因此出现了错误。您需要再次引用正上方的别名子查询:

SELECT p.idproduct, p.name, p.tax,
       (SELECT s.price
        FROM product_storage s
        WHERE s.idproduct=p.idproduct AND s.quantity > 0
        ORDER BY s.added ASC
        LIMIT 1) AS price,
        (       (SELECT s.price
        FROM product_storage s
        WHERE s.idproduct=p.idproduct AND s.quantity > 0
        ORDER BY s.added ASC
        LIMIT 1) * (1 + tax/100)) AS price_with_tax
FROM product p;

将子查询用作表可能会更好:

SELECT p.idproduct, p.name, p.tax, s.price, (s.price * (1 + p.tax/100)) as price_with_tax
FROM product p
INNER JOIN (SELECT idproduct, price
        FROM product_storage
        WHERE quantity > 0) s
ON p.idproduct = s.idproduct
INNER JOIN (SELECT idproduct, MIN(added) min
        FROM product_storage
        GROUP BY idproduct) f
ON s.idproduct = f.idproduct AND s.added = f.min
编辑:已更新,因为tax在另一个表中,所以我必须移动该计算的位置


编辑2:好的,再次更改内容以尝试正确筛选产品存储表。

获取“价格不存在”错误的原因是无法引用列列表中的别名列。要解决此问题,请将价格值存储在用户定义的变量中,并引用如下内容:

SELECT p.idproduct, p.name, p.tax,
       @price := (SELECT s.price
        FROM product_storage s
        WHERE s.idproduct=p.idproduct AND s.quantity > 0
        ORDER BY s.added ASC
        LIMIT 1) AS price,
        (@price * (1 + tax/100)) AS price_with_tax
FROM product p;

首先,您希望获得产品的最低存储id

SELECT idproduct, MIN(idstorage) idstorage FROM product_storage 
WHERE quantity>0 AND idproduct IN (#, #, ...)
GROUP BY idstorage
idstorage,idproduct,added,数量,价格 然后,您可以使用最小存储ID再次加入该查询和product_存储表,以获得具有正确定价信息的产品信息

SELECT idproduct, name, description, tax, ps.price, ps.quantity,
FROM product AS p
JOIN 
(SELECT idproduct, MIN(idstorage) idstorage FROM product_storage 
WHERE quantity>0 AND idproduct IN (#, #, ...)
GROUP BY idstorage) AS min_id
ON p.idproduct=min_id.idproduct
RIGHT JOIN product_storage AS ps ON ps.idstorage=min_id.idstorage
WHERE idproduct IN (#, #, ...)

“product”表中没有任何“price”字段-您确定不会再次计算这个重复的子查询吗?这是我在帖子中没有阐明的一点。您不能使用别名字段来计算税额,因为MySQL正在产品表中查找“价格”列。因此,我写的内容再次明确地要求从Product_存储中获得计算值。无论如何,我认为PowerLord的答案是完全正确的。未知列“字段列表”中的“tax”也许我应该反规范化表并在product_存储中重复tax数据?@Marek:噢,哇,tax在另一个表中,不是吗?是的。。。但它仍然不好-有限制的1子查询是一个有1行的表,没有它的所有行的数量>0。。。特别是,每种不同价格的产品都有不止一行——看看我对这个问题的第一个答案@马瑞克:我应该想到这一点。修复它是可能的,但查询可能会变得更复杂。。。。idproduct IN……-未指定idproduct-例如,我使用最新产品、X类产品等。