mysql_num_row with inner join

1

I have 2 tables:

  • Leads
  • Stores
  • The relationship is as follows: 1 shop - > N leads

    I need to list the number of leads registered for each store and then sort by descending order to know which store has the most leads

    The inner join I am using doubles the stores when you run the query. HELP

        
    asked by anonymous 26.04.2018 / 00:57

    2 answers

    1

    I would do so:

    SELECT nome_loja, uf, (SELECT COUNT(*) FROM leads 
                                   WHERE loja_selecionada = nome_loja) AS 'total'
    FROM lojas ORDER BY total DESC
    

    It also works. =)

    See it working

        
    26.04.2018 / 01:38
    2

    Would it be something like this?

    
    SELECT COUNT(le.id) AS total_leads FROM leads AS le
    INNER JOIN lojas AS lo ON le.id_loja = lo.id
    GROUP BY lo.id
    ORDER BY lo.id DESC
    
    
        
    26.04.2018 / 01:27