Unix Technical Forum

Query which uses more than one value from the same column?

This is a discussion on Query which uses more than one value from the same column? within the MySQL forums, part of the Database Server Software category; --> Hello all, A mysql newbie here with a query-building question that seems simple, but I can't get the syntax ...


Go Back   Unix Technical Forum > Database Server Software > MySQL

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-28-2008, 11:28 AM
Bryan
 
Posts: n/a
Default 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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-28-2008, 11:28 AM
Rik Wasmus
 
Posts: n/a
Default 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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-28-2008, 11:28 AM
Bryan
 
Posts: n/a
Default 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'

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 06:10 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
www.UnixAdminTalk.com