PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.databases.mysql > LEFT JOIN?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
LEFT JOIN?

Réponse
 
LinkBack Outils de la discussion
Vieux 10/04/2008, 14h30   #1
Asteras Amaliadas
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut LEFT JOIN?

Hi,

I have those two tables:

CREATE TABLE Student (
Name INTEGER UNSIGNED NOT NULL,
Gender ENUM('Male','Female') NOT NULL,
PRIMARY KEY(Name)
);

+--------+--------+
| Name | Gender |
+--------+--------+
| John | Male |
| Tom | Male |
| Mary | Female |
| George | Male |
| Kenny | Male |
+--------+--------+

CREATE TABLE Marks (
Name VARCHAR(50) NOT NULL,
Course VARCHAR(500) NOT NULL,
Mark INTEGER UNSIGNED NULL
PRIMARY KEY(Name, Course)
);

+--------+---------+------+
| Name | Course | Mark |
+--------+---------+------+
| John | French | 5 |
| John | Italian | 7 |
| John | Russian | 3 |
| Tom | Russian | 9 |
| Mary | French | 7 |
| Mary | Spanish | 3 |
| Mary | Italian | 9 |
| George | French | 3 |
| George | Russian | 6 |
| George | Italian | 8 |
| Kenny | French | 5 |
| Kenny | Italian | 3 |
+--------+---------+------+

How do I select the boys who passed (scored 5 or higher) the French
and the Italian course? In that case the search result is "John".

Thank you.
  Réponse avec citation
Vieux 10/04/2008, 14h49   #2
Captain Paralytic
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: LEFT JOIN?

On 10 Apr, 14:30, Asteras Amaliadas <aste...@gmail.com> wrote:
> Hi,
>
> I have those two tables:
>
> CREATE TABLE Student (
> Name INTEGER UNSIGNED NOT NULL,
> Gender ENUM('Male','Female') NOT NULL,
> PRIMARY KEY(Name)
> );
>
> +--------+--------+
> | Name | Gender |
> +--------+--------+
> | John | Male |
> | Tom | Male |
> | Mary | Female |
> | George | Male |
> | Kenny | Male |
> +--------+--------+
>
> CREATE TABLE Marks (
> Name VARCHAR(50) NOT NULL,
> Course VARCHAR(500) NOT NULL,
> Mark INTEGER UNSIGNED NULL
> PRIMARY KEY(Name, Course)
> );
>
> +--------+---------+------+
> | Name | Course | Mark |
> +--------+---------+------+
> | John | French | 5 |
> | John | Italian | 7 |
> | John | Russian | 3 |
> | Tom | Russian | 9 |
> | Mary | French | 7 |
> | Mary | Spanish | 3 |
> | Mary | Italian | 9 |
> | George | French | 3 |
> | George | Russian | 6 |
> | George | Italian | 8 |
> | Kenny | French | 5 |
> | Kenny | Italian | 3 |
> +--------+---------+------+
>
> How do I select the boys who passed (scored 5 or higher) the French
> and the Italian course? In that case the search result is "John".
>
> Thank you.


Why have you titled this LEFT JOIN?

What makes you think the answer should be a LEFT JOIN?

What have you already tried?
  Réponse avec citation
Vieux 10/04/2008, 15h14   #3
Asteras Amaliadas
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: LEFT JOIN?

I admit that I am not sure what the right answer is. I thought left
join could be an answer. Ideally, out of those two tables I want to
create a table like that:

+--------+--------+--------+---------+---------+---------+
| Name | Gender | French | Italian | Russian | Spanish |
+--------+--------+--------+---------+---------+---------+
| John | Male | 5 | 7 | NULL | NULL |
| Tom | Male | NULL | NULL | 9 | NULL |
| Mary | Female | 7 | 9 | NULL | 3 |
| George | Male | 3 | 8 | 6 | NULL |
| Kenny | Male | 5 | 3 | NULL | NULL |
+--------+--------+--------+---------+---------+---------+

It is then piece of cake to select the male students who scored >=5 in
French and Italian. This means that I could extent that to many
courses, not just 2.
  Réponse avec citation
Vieux 10/04/2008, 15h40   #4
Captain Paralytic
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: LEFT JOIN?

On 10 Apr, 15:14, Asteras Amaliadas <aste...@gmail.com> wrote:
> I admit that I am not sure what the right answer is. I thought left
> join could be an answer. Ideally, out of those two tables I want to
> create a table like that:
>
> +--------+--------+--------+---------+---------+---------+
> | Name | Gender | French | Italian | Russian | Spanish |
> +--------+--------+--------+---------+---------+---------+
> | John | Male | 5 | 7 | NULL | NULL |
> | Tom | Male | NULL | NULL | 9 | NULL |
> | Mary | Female | 7 | 9 | NULL | 3 |
> | George | Male | 3 | 8 | 6 | NULL |
> | Kenny | Male | 5 | 3 | NULL | NULL |
> +--------+--------+--------+---------+---------+---------+
>
> It is then piece of cake to select the male students who scored >=5 in
> French and Italian. This means that I could extent that to many
> courses, not just 2.


Is there a finite number of courses?
You appear to be looking at a crosstab
  Réponse avec citation
Vieux 10/04/2008, 15h56   #5
Asteras Amaliadas
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: LEFT JOIN?

On 10 Áðñ, 17:40, Captain Paralytic <paul_laut...@yahoo.com> wrote:
> Is there a finite number of courses?
> You appear to be looking at a crosstab


In the real thing, there are 20 of such courses... And even worse,
there are more tables connected to "Student" like
"Marks" ("Studentships", "Activities", etc) and I have to select
students out of all those tables. Total mess!
  Réponse avec citation
Vieux 10/04/2008, 16h44   #6
Captain Paralytic
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: LEFT JOIN?

On 10 Apr, 15:56, Asteras Amaliadas <aste...@gmail.com> wrote:
> On 10 Áðñ, 17:40, Captain Paralytic <paul_laut...@yahoo.com> wrote:
>
> > Is there a finite number of courses?
> > You appear to be looking at a crosstab

>
> In the real thing, there are 20 of such courses... And even worse,
> there are more tables connected to "Student" like
> "Marks" ("Studentships", "Activities", etc) and I have to select
> students out of all those tables. Total mess!


Are you building the queries dynamically using something like php?
  Réponse avec citation
Vieux 10/04/2008, 21h04   #7
Asteras Amaliadas
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: LEFT JOIN?

On Apr 10, 6:44pm, Captain Paralytic <paul_laut...@yahoo.com> wrote:
> Are you building the queries dynamically using something like php?

Yes... But I'd rather not use any scripting tricks...
  Réponse avec citation
Vieux 10/04/2008, 22h29   #8
Paul Lautman
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: LEFT JOIN?

Asteras Amaliadas wrote:
> On Apr 10, 6:44 pm, Captain Paralytic <paul_laut...@yahoo.com> wrote:
>> Are you building the queries dynamically using something like php?

> Yes... But I'd rather not use any scripting tricks...


Wat do you mean by "scripting tricks"?


  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 19h09.


É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,19312 seconds with 16 queries