1

I have following basic MySQL query and I my aim is to artificially inflate the amount of today's orders by 75%.

Is there a more efficient way of doing this without using COUNT twice?

SELECT COUNT(order_id) + (COUNT(order_id) * 0.75) AS total
FROM orders
WHERE order_date BETWEEN BETWEEN CURDATE() + ' 00:00:00' AND NOW()

EDIT: I spotted an issue with my query (using + for concatenation when I should have used CONCAT)

2
  • 1
    CURDATE() + ' 00:00:00' is not what you want -- "+" is not "CONCAT". Anyway CURDATE() performs equally well without the time.
    – Rick James
    Jun 12 '15 at 23:21
  • 2
    5.0 is already ten years old - did you ever think about upgrading to something current?
    – dezso
    Jun 15 '15 at 6:20
5

For general case use your original query as derived table:

SELECT cnt + (cnt * 0.75) AS total
FROM (
    SELECT COUNT(order_id) AS cnt
    FROM orders
    WHERE order_date BETWEEN CURDATE() + ' 00:00:00' AND NOW()
) AS tmp

But in your case it is much simpler:

SELECT (COUNT(order_id) * 1.75) AS total
FROM orders
WHERE order_date BETWEEN CURDATE() + ' 00:00:00' AND NOW()

Not the answer you're looking for? Browse other questions tagged or ask your own question.