Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

people who liked x also liked y

Status
Not open for further replies.

intomuso

Programmer
Jan 26, 2006
19
GB
I'm trying to do a query which is way beyond my usual sql statements and I'm not sure what to use.

I have 3 tables

Songtable (fields - ID,Songname,ArtistID)
SongPaid (fields - SongID,CustID)
ArtistTable (fields - ID, ArtistName)

So what I want to do is look into the "SongPaid" table and see which customers bought that song. The table stucture

SongID CustID
246 312
231 312
133 312
133 345
23 312
23 412
677 412

then look at other customers who bought that track or artist and see what they bought

so for example the song 23 was bought by customers 312 and 412 - who also bought songID 246,231,133 and 617, which are by the artist, find that from songtable.artistid

I need some way of doing this comparison in the songpaid table.

 
What is the relationship between the tables? I see SongID and CustID in SongPaid table, but neither of those are in the other two tables. Or is ID in the other two tables the same as SongID?

-SQLBill

Posting advice: FAQ481-4875
 
Code:
create table SongPaid (SongID int,   CustID int)
--test data
insert into SongPaid values(246,    312)
insert into SongPaid values(231,    312)
insert into SongPaid values(133,    312)
insert into SongPaid values(133,    345)
insert into SongPaid values(23,    312)
insert into SongPaid values(23,    412)
insert into SongPaid values(677,    412)

--here is the query
declare @SongId int
select @SongId = 23

select s1.SongID 
from SongPaid s1
where exists (
select custid from SongPaid
where SongID =@SongId and CustID =s1.CustID )
and SongID <> @SongId

rows returned
133
231
246
677 --not 617, typo?

Denis The SQL Menace
SQL blog:
Personal Blog:
 
Hi,

yes

The SongPaid.SongID - > SongTable.ID
ArtistTable.ID - > SongTable.ArtistID

Hope you can make something out of it

Thanks

gavin
 
This should do it (famous last words....)
Code:
declare @SongId int
select @SongId = 23

select s1.SongID ,st.Songname,a.ArtistName
from SongPaid s1
join Songtable st on s1.SongID = st.ID
join ArtistTable a on st.ArtistID = a.ID
where exists (
select custid from SongPaid
where SongID =@SongId and CustID =s1.CustID )
and SongID <> @SongId

Denis The SQL Menace
SQL blog:
Personal Blog:
 
famous last words :)

select s1.SongID ,st.Songname,a.ArtistName
from SongPaid s1
join Songtable st on s1.SongID = st.ID
join ArtistTable a on st.ArtistID = a.ID
where exists (
select custid from SongPaid
where SongID <> @SongId and CustID =s1.CustID )


r937.com | rudy.ca
 
Hi,

Thanks - I hope the query Where Exists will work in mySQL 3.23
I think the basic ideas you've given can be adapted

declare @SongId int
select @SongId = 23

select s1.SongID ,st.Songname,a.ArtistName

I don't know if you can do that.

Thanks for your help I'll let you know if it works

 
Crap. You are using MySQL???? This forum is for Microsoft's SQL Server. You need to post your question in the MySQL forum.

-SQLBill

Posting advice: FAQ481-4875
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top