Sort table by date

Asked

Viewed 63 times

0

I have a question about how I can get recent purchases by date and time. I tried to:

$stmt = $this->pdo->prepare("SELECT * FROM `website_transactions` ORDER BY `transaction_DATE` AND `transaction_HOUR` DESC LIMIT 8");
$stmt->execute();

He was going to sort by time and date, but he’s just ordering by date, so the result is messy.

the time is in 24 hours format, in case of 00 until 23 and the date in Year-Month-Day format

inserir a descrição da imagem aqui

  • 2

    ORDER BY transaction_DATE , transaction_HOUR

  • The problem is the misuse of AND in values that are not logical. If you want to sort by more than one column, separate with comma.

3 answers

6


The problem is the use of the logical operator AND unduly:

ORDER BY `transaction_DATE` AND `transaction_HOUR` DESC LIMIT 8
                            ^^^

If you want to sort by more than one column, separate by comma, always starting from the order with more priority to the smallest:

ORDER BY expressao1, expressao2, expressao3

Applying to your case:

SELECT * FROM `website_transactions` ORDER BY `transaction_DATE`, `transaction_HOUR` LIMIT 8

If you want to reverse the order, you need to put the DESC (Descending) in all columns as applicable. Example:

nome ASC, transaction_DATE DESC, transaction_HOUR DESC

In this example, the name would be in alphabetical order (crescent), but the date in descending order and the time also (the word ASC is dispensable, I put only to illustrate)

Remembering that there are formats in Mysql that store date and time, but the convenience of using or not these formats depends a lot on how you will use and index the data.

2

Why didn’t you keep the DESC for the two camps?

It is ordering by "asc date" and only "desc time". Do so:

ORDER BY `transaction_DATE` DESC, `transaction_HOUR` DESC

0

A more ideal way would be to leave only one column to store this information... Using the type TIMESTAMP for the column.

Try modifying your table like this:

ALTER TABLE website_transactions DROP COLUMN transaction_DATE;
ALTER TABLE website_transactions DROP COLUMN transaction_HOUR;
ALTER TABLE website_transactions ADD transaction_date TIMESTAMP;

Then you could use this much simpler code to perform your query

SELECT * FROM `website_transactions` ORDER BY `transaction_date` DESC LIMIT 8
  • 1

    Of curiosity, why did you choose to TIMESTAMP and not DATETIME? As an alternative suggestion can be a good one, but be advantageous or does not depend much on how it will use the values (in particular the use in WHERE + indexes).

  • I always had a habit of using TIMESTAMP, so I chose to use the same... When I will insert some value in the table, I use CURRENT_TIMESTAMP

  • 1

    It is worth a read here (not only in the accepted answer) for a broader view of the difference between the two types: https://stackoverflow.com/questions/409286/should-i-use-the-datetime-or-timestamp-datatype-in-mysql

  • 1

    Doing "jabá" from my post, this answer explains a little the difference between DATETIME and TIMESTAMP. Here has a more detailed example, and - more a "self-jabá" - here has an example with code (is in Java, but the idea is to show the problems that can happen with Mysql data types)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.