0

With regards to the usage of the IF statement, which is better to use?

Query #1:

SELECT
    whatever
FROM
    auctions AS a
LEFT JOIN
    ...............
WHERE
    ...............
ORDER BY IF(a.max_bid > a.start_price, a.max_bid, a.start_price) DESC, a.end_time ASC, start_time DESC, a.auction_id ASC;

Query #2:

SELECT
    IF(a.max_bid > a.start_price, a.max_bid, a.start_price) AS current_price
FROM
    auctions AS a
LEFT JOIN
    ...............
WHERE
    ...............
ORDER BY current_price DESC, a.end_time ASC, start_time DESC, a.auction_id ASC;

Or is there a better way to order it?

3
  • Have you looked at the EXPLAIN statement for both?
    – Kermit
    Feb 19 '14 at 18:30
  • @FreshPrinceOfSO Yes and did some tests, the first seems slightly faster, but just was looking for a general consensus in-case things change on larger data sets.
    – Brett
    Feb 19 '14 at 18:43
  • I doubt you'll get a general consensus. Too much is "it depends." You may want to add those types of things (EXPLAIN statements) to your question.
    – Kermit
    Feb 19 '14 at 18:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.