数据库查询根据最后一次开始日期获得最低价格

时间:2018-03-13 14:56:18

标签: mysql sql database wordpress

我希望根据各个经销商的上次抓取日期获得最低价格的产品。我当前的功能是非常基本的,它从表中获得最低价格而不考虑经销商ID和爬行时间戳。

我粗略地认为我们可以SELECT * FROM "custom_data_table"并使用php处理数据。请仔细阅读附件以进一步说明。

function get_lowest_price($table_id) {
    global $wpdb;
    $table_prices = $wpdb->get_results(
        $wpdb->prepare(
            "SELECT price FROM `custom_data_table` WHERE tableid= %d"
            ,$table_id)
    );
    if (!empty($table_prices) && $table_prices !== NULL) 
        return rtrim(min($table_prices)->price, '00');
}

enter image description here

3 个答案:

答案 0 :(得分:2)

这里的正确查询是:

SELECT price
FROM custom_data_name cdn, (
     SELECT MAX(crawled) AS maxCrawled, resellerid
     FROM custom_data_name
     GROUP BY resellerid
) cdnFiltered
WHERE cdn.crawled = cdnFiltered.maxCrawled AND
cdn.resellerid = cdnFiltered.resellerid AND
tableid = %d;

答案 1 :(得分:0)

试试这个:

SELECT B.price 
FROM (SELECT resellerid, MAX(crawled) max_crawled
      FROM custom_data_table
      GROUP BY resellerid) A
JOIN custom_data_table B 
ON A.resellerid=B.resellerid AND A.max_crawled=B.crawled;

答案 2 :(得分:-1)

也许使用ORDER BY抓取和LIMIT 1