E
E
Elizaveta Cezurk2021-07-22 21:30:40
MySQL
Elizaveta Cezurk, 2021-07-22 21:30:40

How to fetch the largest amount from a MYSQL table?

Good day! :)
I have not contacted you for a long time, but I have to return again and again because of my ignorance)
In general, to the point.
I have two tables transaction and user .
The same user can be duplicated in users, leaving his login behind him, but the id changes, in fact, it’s just adding a new user
And in transactions, the user is attached only by id
This code allows me to display the TOP 5 transaction amounts from one user id, but how can I make the same binding by login, if this does not appear in the transaction table in any way?

$select = "SELECT *, SUM(tran_value) as sumvalue FROM `transactions` 
                GROUP BY tran_user_id ORDER BY sumvalue DESC LIMIT 5";
$query = mysqli_query($link,$select) or die("Ошибка: " . mysqli_error($link));

while( $res = mysqli_fetch_assoc($query) ){
  echo 'Сумма: '.$res['sumvalue'].'<br>';
  echo 'ID: '.$res['tran_user_id'].'<br>';	
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Elizaveta Cezurk, 2021-07-22
@semechki

I decided this way, thanks again to FanatPHP and Lazy

SELECT *, SUM(tran_value) as sumvalue FROM `transactions` t LEFT JOIN `users` u ON u.user_id = t.tran_user_id WHERE t.tran_status = 1 GROUP BY u.user_login ORDER BY sumvalue DESC LIMIT 5

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question