简体   繁体   中英

HOW CAN I SELECT ORDERS TABLE?

My table is:

ORDER ID     LINENUMBER      PRODUCT 
----------------------------------------
    XYZ           0          PHONE
    XYZ           1          COMPUTER
    XXX           0          MOUSE
    XXX           1          TV
    XXX           2          COMPUTER
    ZYY           0          TV
    XKX           0          PHONE
    XKX           1          HEADPHONE  

I want products in last 3 orders

ORDER ID    LINENUMBER       PRODUCT
-------------------------------------------
   XKX           1           HEADPHONE 
   XKX           0           PHONE
   ZYY           0           TV 
   XXX           2           COMPUTER  
   XXX           1           TV 
   XXX           0           MOUSE  

Add created_at column to the table with type timestamp with timezone.

Define its value to be by default now() . Now you can pick the last three orders by the latest dates on the table.

On your select query add ORDER BY clause:

...
ORDER BY created_at DESC LIMIT 3;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM