mysql - Query 3 tables at the same time

Asked

Viewed 67 times

0

I have 3 tables, and need to make a SELECT inside a LOOP on 3 at the same time, would be as follows: inserir a descrição da imagem aqui

note that the result, I take the username where it would be in the column NAME, I DO a num_rows to display the quantity of sales and add in SALES and the sum of the products sold by each user.

I found some similar things on the site, but I couldn’t adapt... I tried something like, but I can’t adapt it to my case.

SELECT a.ticket_lote_id, 
       COALESCE(SUM(b.lote_preco), 0) AS lote_preco
  FROM vendidos a LEFT JOIN ingress b ON b.lote_id = a.ticket_id
 GROUP BY a.venda_id 

1 answer

1


try like this :

SELECT 
   u.username, count(v.lote_id) as quantidade_vendas, SUM(p.lote_preco) as total
from USERS u
join VENDAS v on v.user_id = u.username
join PRODUTOS p on p.lote_id = v.lote_id
order by total DESC
Group by u.username;
  • I tested this way, but he returned me a total value of all... not of user per user... I tried to put inside the loop (WHILE) but it still returns me only 1 result and the wrong total value too. (this summing of all users, not each user in a list).

  • edited the query now grouped by username

Browser other questions tagged

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