View Single Post
  #2 (permalink)  
Old Jan 17th, 2007, 09:31
spinal007's Avatar
spinal007 spinal007 is offline
Moderator
Join Date: Mar 2004
Location: Good Ol'London
Age: 23
Posts: 1,650
Blog Entries: 1
Thanks: 0
Thanked 4 Times in 4 Posts
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
Reply With Quote