2016-05-31 19 views
8

Diciamo che ho una tabella:MySQL come ottenere corretto conteggio di tutti i campi desiderati nella tabella

ID, City1, City2, City3, Paese, .... (non è importante)

L'applicazione chiede alle persone dove vorrebbero vivere, diciamo la Francia. Quindi è obbligatorio aggiungere almeno una città, ma è possibile aggiungere 3 città max.

Così, per esempio abbiamo in dati della tabella:

ID City1 City2 City3 Country UserID 
-------------------------------------------------- 
1  Paris / / France  1 
2  Paris Nice  / France  2 
3  Paris Nice  / France  3 
4  Nice  Paris Lyon  France  4 
5  Lyon  Paris Nice  France  5 
6  Cannes Nice  Paris France  6 
7  Paris Cannes Lyon  France  7 
-------------------------------------------------- 

Così ora visualizzare tutti gli utenti su una pagina quando un utente fa clic Francia. Quindi sopra gli utenti voglio visualizzare tutte le città con numero come Parigi (n) per esempio .

quindi se scrivo:

select City1 as city, count(1) as num 
    from table_c 
    where Country = "France" group by City1; 

ottengo Parigi (4), ma ho bisogno di ottenere Parigi (7), perché voglio anche visualizzare City2 e City3, non so come scrivere come una dichiarazione SQL.

Ho provato con molte istruzioni SQL, ma poi ho ottenuto un paio di volte Parigi (n) visualizzato, haw può essere fatto. Se può essere?

+1

fare un UNION ALL. (Oppure usa le espressioni del caso.) – jarlh

+0

come può essere fatto, non ho molta esperienza con SQL, è possibile che tu possa scrivermi un esempio per favore. Grazie – mpotoc

+2

'SELEZIONA città, COUNT (*) FROM (SELEZIONA paese, città1 AS città FROM my_table UNIONE TUTTO Seleziona paese, città2 FROM my_table UNIONE TUTTO Seleziona paese, città3 FROM my_table) x GROUP BY city'. Si noti che le colonne enumerate (sopra 2, per esempio) sono spesso sintomatiche di scarso design – Strawberry

risposta

2

si può provare questo sql e fammi sapere se questo funziona

select city, count(1) as num from (
select City1 as city 
from table_c 
where Country = "France" and city1 is not null 
UNION ALL 
select City2 as city 
from table_c 
where Country = "France" and city2 is not null 
UNION ALL 
select City3 as city 
from table_c 
where Country = "France" and city3 is not null 
) tbl 
group by city 
+1

Questa è una copia di più di una risposta qui. – sagi

+0

Ho appena visto che c'erano più risposte delle mie –

3

In caso CITY1 colonna contiene tutte le città, allora si può semplicemente fare questo:

SELECT t.city1,sum(t.city1 = t.city2 + t.city1 = t.city3 + t.city1 = t.city4) 
FROM table_c t 
WHERE t.Country = "France" 
GROUP BY t.city1 

caso contrario, utilizzare UNION ALL:

SELECT t.city,count(*) FROM (
    SELECT City1 as city FROM table_c WHERE Country = "France" 
    UNION ALL 
    SELECT City2 FROM table_c WHERE Country = "France" 
    UNION ALL 
    SELECT City3 FROM table_c WHERE Country = "France" 
    UNION ALL 
    SELECT City4 FROM table_c WHERE Country = "France") t 
GROUP BY t.city 

questo vi darà i risultati corretti

2

Si potrebbe provare la seguente domanda:

SELECT  city, 
      SUM(cnt) AS num 
    FROM (
     SELECT  City1 AS city, 
        COUNT(*) AS cnt 
      FROM table_c 
      WHERE Country = 'France' 
     GROUP BY City1 
     UNION ALL 
     SELECT  City2 AS city, 
        COUNT(*) AS cnt 
      FROM table_c 
      WHERE Country = 'France' 
     GROUP BY City2 
     UNION ALL 
     SELECT  City3 AS city, 
        COUNT(*) AS cnt 
      FROM table_c 
      WHERE Country = 'France' 
     GROUP BY City3 
     ) tmp 
GROUP BY tmp.city; 
2

Prova questa;)

select city, count(1) 
from (
    select City1 as city 
    from table_c 
    where Country = "France" 
    union all 
    select City2 as city 
    from table_c 
    where Country = "France" 
    union all 
    select City3 as city 
    from table_c 
    where Country = "France") tmp 
group by city