PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Logiciels d'hébergement > mailing.database.mysql > Displaying table as grid
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Displaying table as grid

Réponse
 
LinkBack Outils de la discussion
Vieux 26/03/2006, 15h57   #1
Sardaukary@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Displaying table as grid

Hello,

>From the following table showing which countries people have been to


Name Countries

Fred France
Fred UK
Bill Germany
Jack USA
Bill UK
Tim USA
Tim France
Tim Italy

what SQL statement can I use in MySQL to format it like this

Name France Italy Germany UK USA
Bill Yes Yes
Fred Yes Yes
Jack Yes
Tim Yes Yes Yes

It's like an excel pivot table but lots of googling for things like
that in SQL found nothing.

  Réponse avec citation
Vieux 26/03/2006, 21h46   #2
Bill Karwin
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Displaying table as grid

<Sardaukary@gmail.com> wrote in message
news:1143388677.989608.156150@i40g2000cwc.googlegr oups.com...
> Hello,
>
>>From the following table showing which countries people have been to

>
> Name Countries
>
> Fred France
> Fred UK
> Bill Germany
> Jack USA
> Bill UK
> Tim USA
> Tim France
> Tim Italy
>
> what SQL statement can I use in MySQL to format it like this
>
> Name France Italy Germany UK USA
> Bill Yes Yes
> Fred Yes Yes
> Jack Yes
> Tim Yes Yes Yes


Well, I can suggest a couple of solutions that work for the example of the
five specific countries you give above.

SELECT t.Name,
SUM(IF(t.Countries = 'France', 1, 0)) AS `France`,
SUM(IF(t.Countries = 'Italy', 1, 0)) AS `Italy`,
SUM(IF(t.Countries = 'Germany', 1, 0)) AS `Germany`,
SUM(IF(t.Countries = 'UK', 1, 0)) AS `UK`,
SUM(IF(t.Countries = 'USA', 1, 0)) AS `USA`
FROM myTable AS t
GROUP BY t.Name

The above solution is based on the article at
http://dev.mysql.com/tech-resources/...ard/index.html.

Here's another possibility:

SELECT DISTINCT t.Name,
IF(c1.Countries IS NULL, '', 'Yes'),
IF(c2.Countries IS NULL, '', 'Yes'),
IF(c3.Countries IS NULL, '', 'Yes'),
IF(c4.Countries IS NULL, '', 'Yes'),
IF(c5.Countries IS NULL, '', 'Yes')
FROM myTable AS t
LEFT OUTER JOIN myTable AS c1 ON t.Name = c1.Name AND c1.Countries =
'France'
LEFT OUTER JOIN myTable AS c2 ON t.Name = c2.Name AND c2.Countries =
'Italy'
LEFT OUTER JOIN myTable AS c3 ON t.Name = c3.Name AND c3.Countries =
'Germany'
LEFT OUTER JOIN myTable AS c4 ON t.Name = c4.Name AND c4.Countries =
'UK'
LEFT OUTER JOIN myTable AS c5 ON t.Name = c5.Name AND c5.Countries =
'USA';

The solutions above might not work well in your case, because you need to
hardcode the country names. I assume the list of countries could grow, so
hardcoding them in the query is not going to be work. Most crosstab or
pivot-table solutions I have read rely on the set of columns being fixed and
relatively few in number.

Another option is to use GROUP_CONCAT:

SELECT t.Name, GROUP_CONCAT(t.Countries ORDER BY t.Countries) AS Countries
FROM myTable AS t
GROUP BY t.Name

This outputs two columns: the name, and a string containing the
comma-separated list of countries:

Bill Germany,UK
Fred France,UK
Jack USA
Tim France,Italy,USA

You'd have to split the comma-separated string in your application to put
the values into the proper columns to make it into a grid. But this
solution does work even if the number of distinct countries grows. No need
to hard-code the countries in the SQL query.

Regards,
Bill K.


  Réponse avec citation
Vieux 28/03/2006, 20h52   #3
Sardaukary@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Displaying table as grid

Thanks. That worked perfectly! :-)

Is it possible to sort the columns produced?

  Réponse avec citation
Vieux 28/03/2006, 21h36   #4
Bill Karwin
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Displaying table as grid

On Tue, 28 Mar 2006 12:52:19 -0800, <Sardaukary@gmail.com> wrote:

> Is it possible to sort the columns produced?


Sort by which columns? t.Name or the result of GROUP_CONCAT()?

You can always "ORDER BY 2" to sort by the second column in the
select-list, even if it's an expression and you haven't given it an alias.

Regards,
Bill K.
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 05h09.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,10169 seconds with 12 queries