简体   繁体   English

如何在mysql查询中获取最新的商品价格?

[英]How can I get the latest item price in mysql query?

I manage to get the lowest, highest and average price of the item but couldn't get the latest price.我设法获得了该商品的最低、最高和平均价格,但无法获得最新价格。 Below is the select query i am using by joining the item and item_price tables.下面是我通过加入 item 和 item_price 表使用的选择查询。 How can I fix the problem?我该如何解决这个问题?

$sql = 'SELECT *, MIN(ip_price) AS lowest_price, MAX(ip_price) AS highest_price, 
        AVG(ip_price) AS average_price, MAX(ip_price_date) AS latest_date,
        (SELECT ip_price FROM cnf_item_price WHERE ip_price_date = "latest_date") AS latest_price
        FROM cnf_item
        INNER JOIN cnf_item_price ON cnf_item_price.ip_item_id = cnf_item.it_id
        WHERE 1 AND cnf_item_price.ip_supp_id=?
        GROUP BY cnf_item.it_id
        ORDER BY cnf_item.it_name ASC';
$stmt = $DB->prepare($sql);
$stmt->bindValue(1,$supplier_id);
$stmt->execute();

can you try following query:您可以尝试以下查询吗:

$sql = 'SELECT *, MIN(ip_price) AS lowest_price, MAX(ip_price) AS highest_price, 
        AVG(ip_price) AS average_price, MAX(ip_price_date) AS latest_date,
        (SELECT ip_price FROM cnf_item_price order by ip_price_date desc limit 1) AS latest_price
        FROM cnf_item
        INNER JOIN cnf_item_price ON cnf_item_price.ip_item_id = cnf_item.it_id
        WHERE 1 AND cnf_item_price.ip_supp_id=?
        GROUP BY cnf_item.it_id
        ORDER BY cnf_item.it_name ASC';
$stmt = $DB->prepare($sql);
$stmt->bindValue(1,$supplier_id);
$stmt->execute();

Thanks #ahmet kamaran.谢谢#ahmet kamaran。 It's working for my case here.它在这里适用于我的情况。 I also replace * with those columns needed as suggested by others.我还将 * 替换为其他人建议的所需列。 So, after some testing from the above suggestions, here is my codes which retrieve those data i needed.因此,经过上述建议的一些测试后,这是我的代码,用于检索我需要的那些数据。

$sql = 'SELECT it_id, it_code, it_name, it_desc, 
        ip_id, ip_item_id, ip_supp_id, ip_price, ip_price_date, ip_ref_no, ip_remarks, 
        MIN(ip_price) AS lowest_price, MAX(ip_price) AS highest_price,
        AVG(ip_price) AS average_price, MAX(ip_price_date) AS latest_date,
        (SELECT ip_price FROM cnf_item_price ORDER BY ip_price_date DESC LIMIT 1) AS latest_price
        FROM cnf_item
        INNER JOIN cnf_item_price ON cnf_item_price.ip_item_id = cnf_item.it_id
        WHERE 1 AND cnf_item_price.ip_supp_id=?
        GROUP BY cnf_item.it_id
        ORDER BY cnf_item.it_name ASC';

$stmt = $DB->prepare($sql);
$stmt->bindValue(1,$supplier_id);
$stmt->execute();

http://sqlfiddle.com/#!9/ca6234/2 http://sqlfiddle.com/#!9/ca6234/2

Thanks.谢谢。

SELECT it_id, it_code, it_name, it_desc, ip_id, ip_item_id, ip_supp_id, ip_price, ip_price_date, ip_ref_no, ip_remarks, MIN(ip_price) AS lowest_price, MAX(ip_price) AS highest_price, AVG(ip_price) AS average_price, MAX(ip_price_date) AS latest_date, (SELECT ip_price FROM cnf_item_price WHERE cnf_item_price.ip_item_id = cnf_item.it_id ORDER BY ip_price_date DESC LIMIT 1) AS latest_price FROM cnf_item INNER JOIN cnf_item_price ON cnf_item_price.ip_item_id = cnf_item.it_id WHERE 1 GROUP BY cnf_item.it_id ORDER BY cnf_item.it_name ASC;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM