WHERE SQL Functions

0

I have the following query:

$qrysel = "select * from won_auctions w left join registration r on w.userid=r.id where xxx;

This query will show a list of users of the won_auctions table and get the user name in the registration table.

The idea is to check how many records each user has in the won_auctions table during the current month, and the total.

I also need to sort to display the ones with the most records first, for the current month.

    
asked by anonymous 26.11.2017 / 14:45

2 answers

1
SELECT
    COUNT(*) AS qtd_reg,
    w.*,
    r.*
FROM
    won_auctions w
LEFT JOIN registration r ON w.userid = r.id
WHERE
    YEAR([campo_data]) = '2017' -- ANO DO FILTRO
    AND MONTH([campo_data]) = '11' -- MES DO FILTRO;
GROUP BY [user_id]
ORDER BY qtd_reg
    
27.11.2017 / 14:15
0

This is an option, as you did not give many details about your architecture or how your tables are, I used generic names:

select * from (
 select r.username, count(w.registros) as contagem
 from won_auctions w 
 left join registration r 
  on w.userid=r.id where data >= 'inicio do mes' and data <= 'final do mes'
 group by r.userid
) a
order by a.contagem desc
    
26.11.2017 / 16:05