2

I want to fetch all the records whose transaction_date falls today e.g. (12th February 2014). So to achieve this I tried following SQL query:

SELECT transaction_status, count( * ) AS StatusCount
FROM OCN.user_transaction
WHERE transaction_date = date('Ymd')
GROUP BY transaction_status

But it's not working for me. Can anyone help me in this regard please? Thanks in advance.

4
  • 1
    What is the column type of transaction_date ? Commented Feb 12, 2014 at 10:53
  • how is transaction date saved in db? date or datetime or timestamp? Commented Feb 12, 2014 at 10:55
  • @Rikesh:The data type is bigint(12) Commented Feb 12, 2014 at 10:55
  • Presuming UNIX timestamp, presuming MySQL; select ... where date(now()) = date(from_unixtime(transaction_date)) ...;. Commented Feb 12, 2014 at 11:06

6 Answers 6

3

If data type is BigInt(12) then this should work for you:

SELECT
    transaction_status, 
    count( * ) AS StatusCount 
FROM 
    OCN.user_transaction 
WHERE 
    DATE_FORMAT(FROM_UNIXTIME(transaction_date), "%Y-%m-%d") = DATE(NOW()) 
GROUP BY 
    transaction_status

But using BigInt to store timestamps is not the best way to store date in MySQL. You can use DATE, DATETIME or TIMESTAMP, depending on your needs.

Sign up to request clarification or add additional context in comments.

Comments

1

You would do worse than to use the CURDATE function in MySQL:

SELECT 
  transaction_status,
  COUNT(*) AS StatusCount 
FROM
  OCN.user_transaction 
WHERE transaction_date = CURDATE()
GROUP BY transaction_status

If you've further questions, do leave a comment.

Comments

1

You are comparing 2 different date-time formats, so parse the 'transaction_date' to the same date-time format as the input date. Use DATE_FORMAT(date,format) for this problem.

example implementation:

    SELECT transaction_status, 
         count( * ) AS StatusCount 
    FROM OCN.user_transaction 
   WHERE DATE_FORMAT(transaction_date, "Ymd") = date('Ymd') 
GROUP BY transaction_status

Good luck!

Comments

1

Try this:

SELECT 
transaction_status,
COUNT(*) AS StatusCount 
FROM
OCN.user_transaction 
WHERE transaction_date =date_format(CURRENT_TIMESTAMP, "%d-%m-%y")
GROUP BY transaction_status ;

You can modify the format as you want.

Comments

1

The current date method returns today's date...So i believe the post that was submitted by hd1 should work.

SELECT transaction_status, COUNT(*) AS StatusCount FROM OCN.user_transaction WHERE transaction_date = CURDATE() GROUP BY transaction_status

Comments

-2

if transaction_date is timestamp type, and query create by php, try this:

SELECT 
  transaction_status,
  COUNT(*) AS StatusCount 
FROM
  OCN.user_transaction 
WHERE transaction_date = <?php time() ?>
GROUP BY transaction_status 

else try mysql now() function

SELECT 
  transaction_status,
  COUNT(*) AS StatusCount 
FROM
  OCN.user_transaction 
WHERE transaction_date = now()
GROUP BY transaction_status 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.