0

I want to order by price. But I am getting the price from two different table and compare it to get the minimum price and print it if price is above 0. It is also possible that price is present in only one database. The rough structure of my code is:

query1 = choose products

while = fetch the products details {

query2 = get the price1, order by price1
query3 = get the price2, order by price2

if statement to get the minimum price so price = minimum price

if (price>0) {
    echo product result
}

}

How can I order the result according to the price?

EDIT: Product is already selected, i want to acquire the price of products from two different price table

4

1 回答 1

0

You can do it in just one query, much more efficiently:

SELECT `a`.*, LEAST(`b`.`price`,`c`.`price`) AS `price`
FROM `products` `a`
JOIN `price1` `b` ON `a`.`id`=`b`.`product_id`
JOIN `price2` `c` on `a`.`id`=`c`.`product_id`
WHERE `price`>0
ORDER BY `price` ASC

Adjust according to your database's tables and columns.

于 2013-09-28T12:54:46.800 回答