How to make a mysql query for php graph?

1

I need to make a query in MYSQL, I have the following data

COLUM1  FECHA   PAIS
0   2018-11-13  
0   2018-11-13  Reino Unido
0   2018-11-13  
1   2018-11-13  Colombia
0   2018-11-13  
1   2018-11-13  Peru
0   2018-11-13  
0   2018-11-13  Espana
1   2018-11-13  
0   2018-11-13  Colombia
0   2018-11-13  
0   2018-11-13  USA
0   2018-11-13  
1   2018-11-13  Brasil
0   2018-11-13  
0   2018-11-13  Uruguay
0   2018-11-13  
0   2018-11-13  Peru
0   2018-11-13  
0   2018-11-13  
0   2018-11-13  Costa Rica
0   2018-11-13  Costa Rica
0   2018-11-13  
1   2018-11-13  
0   2018-11-13  
0   2018-11-13  USA
0   2018-11-13  
0   2018-11-13  Peru
1   2018-11-13  
0   2018-11-13  USA
0   2018-11-13  
0   2018-11-13  USA
0   2018-11-13  
0   2018-11-13  USA
1   2018-11-13  
0   2018-11-13  
0   2018-11-13  USA
0   2018-11-13  
0   2018-11-13  Costa Rica
0   2018-11-13  
0   2018-11-13  Argentina
0   2018-11-13  
0   2018-11-13  Argentina
0   2018-11-13  
0   2018-11-13  Paraguay
0   2018-11-13  
0   2018-11-13  Peru
0   2018-11-13  
0   2018-11-13  Colombia
1   2018-11-13  
1   2018-11-13  
1   2018-11-13  Argentina
0   2018-11-13  
0   2018-11-13  Peru
0   2018-11-13  
0   2018-11-13  Colombia

In the COLUM1 column you can see 0 and 1 where 0 represents EXITOSO and 1 NO EXITOSO

What I need to bring me is the following:

  • The total of EXITOSO for each country
  • The total of NO EXITOSO for each country
asked by Juan Perez 14.11.2018 в 13:25
source

1 answer

0

You can get the totals in different queries:

  

The total of countries

select count(*) as cant from (select distinct pais from <tu tabla>) as p;
  

The total SUCCESS for each country

select count(*) as cant, pais from <tu tabla> where column1 = 0 group by pais;
  

The total of NOT SUCCESSFUL for each country

select count(*) as cant, pais from <tu tabla> where column1 = 1 group by pais;
  

And can it be done only by taking out the unsuccessful and successful country?

To put the number of successful and unsuccessful in the same row you have to do a JOIN between the two subqueries. If all the countries appear in the two subqueries, a join can be made between the two tables without loss of data. But doing this if any country appears only in one of the two subqueries, that country will not appear in the final result.

To have all the results, whether the country is on one side, on the other, or both, we would have to use a FULL JOIN . But Mysql does not have FULL JOIN. In an article I found that it can be simulated with the LEFT and RIGHT JOIN that are below.

select COALESCE(exitoso.paise, noexitoso.paisne) Pais, cante, cantne
from (
    select *
    from (select count(*) as cante, pais paise from <tu tabla> where column1 = 0 group by pais) as exitoso
    left join
         (select count(*) as cantne, pais paisne from <tu tabla> where column1 = 1 group by pais) as noexitoso
    on (exitoso.paise = noexitoso.paisne)
    union all
    select * 
    from (select count(*) as cante, pais paise from <tu tabla> where column1 = 0 group by pais) as exitoso
    right join
         (select count(*) as cantne, pais paisne from <tu tabla> where column1 = 1 group by pais) as noexitoso
    on (exitoso.paise = noexitoso.paisne)
    where exitoso.paise is null
) as exitopais
order by COALESCE(exitoso.paise, noexitoso.paisne)
    
answered by 14.11.2018 в 13:32