mysql - SQL order by date descending expression -


i have 2 tables, worker , worker_balance:

---------------           ------------------ |  worker     |           | worker_balance | |-------------|<----------|----------------| |             |           | int workerbid  | | ...         |           | int amount     | ---------------           | int workerid   |                           | date timestamp |                           ------------------ 

and following sql-query:

select worker.id, worker.xy, sum(worker_balance.amount) worker join worker_balance on workerbalance.workerid = worker.workerid group worker.id, worker.xy 

i need results ordered latest timestamp, the oldest must first. worker, last transaction oldest, comes first.

you need group by in order aggregate , order by in order sort:

select worker.id     ,worker.xy     ,max(worker.timestamp) last_transaction     ,sum(worker_balance.amount) worker join worker_balance on workerbalance.workerid = worker.workerid group worker.id     ,worker.xy order last_transaction 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -