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 > Query which uses more than one value from the same column?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Query which uses more than one value from the same column?

Réponse
 
LinkBack Outils de la discussion
Vieux 26/09/2007, 04h44   #1
Bryan
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Query which uses more than one value from the same column?

Hello all,

A mysql newbie here with a query-building question that seems simple,
but I can't get the syntax right.

Say that I have three tables with the following columns...


Songs:
-----
song_id
song_title


Singers:
-------
singer_id
first_name


Singers_in_Songs:
----------------
id
song_id
singer_id

(the Singers_in_Songs join table allowing for normalization: one
singer could have many songs, and one song could have many singers
performing in it.)

I know the SELECT to find all the songs featuring a certain singer,
e.g.:

SELECT song_title FROM Songs JOIN Singers_in_Songs USING (song_id)
JOIN Singer USING (singer_id) WHERE Singers.first_name = 'Lucy';

My question is: How do I modify this query to get all song titles in
which 'Lucy' AND 'Linus' are singing, but ONLY those titles in which
they BOTH appear?



Thanks for any hints,
B.
  Réponse avec citation
Vieux 26/09/2007, 08h05   #2
Rik Wasmus
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Query which uses more than one value from the same column?

On Wed, 26 Sep 2007 05:44:40 +0200, Bryan <wendelnNOSPAMMING@gmail.com>
wrote:

> Hello all,
>
> A mysql newbie here with a query-building question that seems simple,
> but I can't get the syntax right.
>
> Say that I have three tables with the following columns...
>
>
> Songs:
> -----
> song_id
> song_title
>
>
> Singers:
> -------
> singer_id
> first_name
>
>
> Singers_in_Songs:
> ----------------
> id
> song_id
> singer_id
>
> (the Singers_in_Songs join table allowing for normalization: one
> singer could have many songs, and one song could have many singers
> performing in it.)
>
> I know the SELECT to find all the songs featuring a certain singer,
> e.g.:
>
> SELECT song_title FROM Songs JOIN Singers_in_Songs USING (song_id)
> JOIN Singer USING (singer_id) WHERE Singers.first_name = 'Lucy';
>
> My question is: How do I modify this query to get all song titles in
> which 'Lucy' AND 'Linus' are singing, but ONLY those titles in which
> they BOTH appear?


Perhaps something like this (untested):

SELECT songs.id, songs.song_title
FROM songs
JOIN singers_in_songs sis1
ON sis1.song_id = songs.id
JOIN singers s1
ON s1.id = sis1.singer_id
AND s1.first_name = 'Lucy'
JOIN singers_in_songs sis2
ON sis2.song_id = songs.id
JOIN singers s2
ON s2.id = sis2.singer_id
AND s2.first_name = 'Linus'
--
Rik Wasmus
  Réponse avec citation
Vieux 27/09/2007, 10h58   #3
Bryan
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Query which uses more than one value from the same column?

Yes, that did it. Many thanks. I was stuck under the mistaken
assumption that I had to have a WHERE clause in there someplace.
Thanks again.

B.

On Wed, 26 Sep 2007 09:05:01 +0200, "Rik Wasmus"
<luiheidsgoeroe@hotmail.com> wrote:

>On Wed, 26 Sep 2007 05:44:40 +0200, Bryan <wendelnNOSPAMMING@gmail.com>
>wrote:
>
>> Hello all,
>>
>> A mysql newbie here with a query-building question that seems simple,
>> but I can't get the syntax right.
>>
>> Say that I have three tables with the following columns...
>>
>>
>> Songs:
>> -----
>> song_id
>> song_title
>>
>>
>> Singers:
>> -------
>> singer_id
>> first_name
>>
>>
>> Singers_in_Songs:
>> ----------------
>> id
>> song_id
>> singer_id
>>
>> (the Singers_in_Songs join table allowing for normalization: one
>> singer could have many songs, and one song could have many singers
>> performing in it.)
>>
>> I know the SELECT to find all the songs featuring a certain singer,
>> e.g.:
>>
>> SELECT song_title FROM Songs JOIN Singers_in_Songs USING (song_id)
>> JOIN Singer USING (singer_id) WHERE Singers.first_name = 'Lucy';
>>
>> My question is: How do I modify this query to get all song titles in
>> which 'Lucy' AND 'Linus' are singing, but ONLY those titles in which
>> they BOTH appear?

>
>Perhaps something like this (untested):
>
>SELECT songs.id, songs.song_title
> FROM songs
>JOIN singers_in_songs sis1
> ON sis1.song_id = songs.id
>JOIN singers s1
> ON s1.id = sis1.singer_id
> AND s1.first_name = 'Lucy'
>JOIN singers_in_songs sis2
> ON sis2.song_id = songs.id
>JOIN singers s2
> ON s2.id = sis2.singer_id
> AND s2.first_name = 'Linus'

  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 23h20.


É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,10623 seconds with 11 queries