2

I would like to get lowest price of product based on last crawled dates by various resellers. My current function is very basic, it gets me lowest price from table without considering reseller ids and crawled timestamps.

I've rough idea that we can SELECT * FROM "custom_data_table" and process the data using php. Please have a look at attachment for further clarification.

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

Daniele Murer
  • 247
  • 3
  • 9
wp student
  • 755
  • 9
  • 24

3 Answers3

2

The right query here is:

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;
Daniele Murer
  • 247
  • 3
  • 9
0

Try this:

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;
cdaiga
  • 4,861
  • 3
  • 22
  • 42
-1

Maybe use ORDER BY crawled and LIMIT 1