You are here:  » Cheapest Price SQL

Support Forum



Cheapest Price SQL

Submitted by dbfcs on Sat, 2006-12-16 11:58 in

Can you tell me what the actual code is that extracts the cheapest price on the search results page?

In other words, I am after the piece of SQL that finds the cheapest price in the database for a given product.

Thanks!

Submitted by support on Sun, 2006-12-17 03:48

Hi Dave,

The SQL isn't just built on one line; it is constructed over the course of several lines of code considering the query type (normal search, merchant: etc.), the sort order and page / offset.

The main bulk of the SQL is generated in each of the switch cases (lines 36 though 62 in the distribution), and for a default product search it is the code after the "default:" case.

The ordering SQL is then added to the base SQL (the core query that selects the products) on line 84:

$sql .= " ORDER BY ".$orderBySelection[$sort];

...where $orderBySelection[$sort] is "minPrice ASC" - minPrice being a summary value created in the main SQL as follows:

SELECT * , MIN( price ) AS minPrice

If you want to study the complete query, the easiest thing to do would be to print it out, round about line 90, just before:

database_querySelect($sqlResultCount,$rows);

...for example:

print "<p>".$sql."</p>";

Hope this helps!
Cheers,
David.