Unix Technical Forum

I know this involves LEFT JOINs somehow

This is a discussion on I know this involves LEFT JOINs somehow within the MySQL forums, part of the Database Server Software category; --> Hi, I have two tables SUBSCRIPTION_TYPES ----------------------------------- ID INTEGER NAME VARCHAR(255) and SUBSCRIPTIONS ------------------------- iD INTEGER SUBSCRIPTION_ID INTEGER USER_ID ...


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:12 AM
laredotornado@zipmail.com
 
Posts: n/a
Default I know this involves LEFT JOINs somehow

Hi,

I have two tables

SUBSCRIPTION_TYPES
-----------------------------------
ID INTEGER
NAME VARCHAR(255)

and

SUBSCRIPTIONS
-------------------------
iD INTEGER
SUBSCRIPTION_ID INTEGER
USER_ID INTEGER

If a row appears in the SUBSCRIPTIONS table, it means a user has a
subscription to a particular item. If no appears for that user, he
has no subscription. I'm trying to write a query that, for a given
user, will return 1 or 0 based on whether or not the user has a
subscription. How would I do this?

I'm using MySQL 5.0.

Thanks, - Dave

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-28-2008, 11:12 AM
Adam Englander
 
Posts: n/a
Default Re: I know this involves LEFT JOINs somehow

laredotornado@zipmail.com wrote:
> Hi,
>
> I have two tables
>
> SUBSCRIPTION_TYPES
> -----------------------------------
> ID INTEGER
> NAME VARCHAR(255)
>
> and
>
> SUBSCRIPTIONS
> -------------------------
> iD INTEGER
> SUBSCRIPTION_ID INTEGER
> USER_ID INTEGER
>
> If a row appears in the SUBSCRIPTIONS table, it means a user has a
> subscription to a particular item. If no appears for that user, he
> has no subscription. I'm trying to write a query that, for a given
> user, will return 1 or 0 based on whether or not the user has a
> subscription. How would I do this?
>
> I'm using MySQL 5.0.
>
> Thanks, - Dave
>

If yo have a user table, an I assume you do, you could us something like
this:
select u.name,if(s.id is null,0,1) from user u left join subscriptions s
on u.id = s.user_id;

Adam
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-28-2008, 11:12 AM
subtenante
 
Posts: n/a
Default Re: I know this involves LEFT JOINs somehow

On Tue, 10 Jul 2007 14:45:44 -0700, "laredotornado@zipmail.com"
<laredotornado@zipmail.com> wrote:

>Hi,
>
>I have two tables
>
>SUBSCRIPTION_TYPES
>-----------------------------------
>ID INTEGER
>NAME VARCHAR(255)
>
>and
>
>SUBSCRIPTIONS
>-------------------------
>iD INTEGER
>SUBSCRIPTION_ID INTEGER
>USER_ID INTEGER
>
>If a row appears in the SUBSCRIPTIONS table, it means a user has a
>subscription to a particular item. If no appears for that user, he
>has no subscription. I'm trying to write a query that, for a given
>user, will return 1 or 0 based on whether or not the user has a
>subscription. How would I do this?
>
>I'm using MySQL 5.0.
>
>Thanks, - Dave


It seems you have all information needed in SUBSCRIPTIONS, no need for
a join here...

Something like :

SELECT IF(count(*)>0, 1, 0)
FROM `subscriptions`
WHERE `user_id`='...';
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 04:47 PM.


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