help with some sql

This is a discussion on "help with some sql" within the Databases section. This forum, and the thread "help with some sql are both part of the Program Your Website category.



Go Back   Webforumz.com > Main Forums > Program Your Website > Databases

Notices


Reply
 
LinkBack Thread Tools
  #1 (permalink)  
Old Jan 16th, 2007, 17:51
Junior Member
Join Date: Mar 2006
Location: uk
Age: 26
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
help with some sql

Hi,

i need some help with a query. Ive got 3 tables, one for the magazin(uid, name, price), subscriber (uid, firstname, lastname) and subscription(personID, magazineuid).

What i want to do is perform a query that lets me show all the names of the magazine in one column and whether or not a specific person is subscribed to that magazine. so it would somethin like this:

mags subscriptions for joe bloggs:

mag name---- subscribed
mag 1 ----------yes
mag 2 ----------yes
mag 3 ----------no
..etc

any ideas?
Reply With Quote

  #2 (permalink)  
Old Jan 17th, 2007, 09:31
spinal007's Avatar
Moderator
Join Date: Mar 2004
Location: Good Ol'London
Age: 22
Posts: 1,620
Blog Entries: 1
Thanks: 0
Thanked 2 Times in 2 Posts
Send a message via ICQ to spinal007 Send a message via MSN to spinal007 Send a message via Yahoo to spinal007 Send a message via Skype™ to spinal007
Re: help with some sql

Create a function that checks for the existence of a record in the subscription table.

ie.: (not properly coded)
function IsSubscribed(@person,@mag)
if exists(select personID from where personID=@person and magazineuid=@mag)
return 'y'
else
return 'n'


then use...
SELECT m.name AS 'magazine', p.name AS 'person', IsSubscribed(p.personID , m.magazineuid) AS 'subscribed'
FROM magazin m INNER JOIN subscriber p ON 1=1

This will join where subscriber to every magazine and check if they're subscribed. Filter it properly to check only the ones you want to...

eg.:
SELECT m.name AS 'magazine', p.name AS 'person', IsSubscribed(p.personID , m.magazineuid) AS 'subscribed'
FROM magazin m INNER JOIN subscriber p ON p.personID=999


SELECT m.name AS 'magazine', p.name AS 'person', IsSubscribed(p.personID , m.magazineuid) AS 'subscribed'
FROM magazin m INNER JOIN subscriber p ON m.magazineuid=999
Last Blog Entry: Random String in Javascript (Apr 21st, 2008)
Reply With Quote
  #3 (permalink)  
Old Jan 17th, 2007, 10:41
Junior Member
Join Date: Mar 2006
Location: uk
Age: 26
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Re: help with some sql

thanks for that but i need to do it in a single sql query.
Reply With Quote
  #4 (permalink)  
Old Jan 17th, 2007, 12:58
spinal007's Avatar
Moderator
Join Date: Mar 2004
Location: Good Ol'London
Age: 22
Posts: 1,620
Blog Entries: 1
Thanks: 0
Thanked 2 Times in 2 Posts
Send a message via ICQ to spinal007 Send a message via MSN to spinal007 Send a message via Yahoo to spinal007 Send a message via Skype™ to spinal007
Re: help with some sql

ok, that was ideal for a view
here's how to do it in one query:

Code: Select all
SELECT
   m.magazineuid, p.personID
   m.name AS 'magazine name',
   p.name AS 'person name', 
 CASE
  WHEN
    EXISTS(SELECT magazineuid FROM subscription WHERE personID=@personID AND magazineuid=@magazineuid)
  THEN 'Y'
  ELSE 'N'
 END
 AS 'subscribed'
FROM magazin m INNER JOIN subscriber p ON 1=1
Last Blog Entry: Random String in Javascript (Apr 21st, 2008)
Reply With Quote
Reply

Tags
sql, mysql

Thread Tools

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 05:09.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43