(simple?) query dilemma--help please

This is a discussion on (simple?) query dilemma--help please within the MySQL Database forums, part of the Database Forums category; This should be easy, but I've been fudging with this for a while so perhaps someone can help. I ...


Go Back   Usenet Forums > Database Forums > MySQL Database

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 11-19-2005
Bosconian
 
Posts: n/a
Default (simple?) query dilemma--help please

This should be easy, but I've been fudging with this for a while so
perhaps
someone can help.

I have 2 tables: fruit, fruitchoices.

The first (fruit) is a read-only lookup table:

fid, fruit

1, apple
2, banana
3, orange
4, peach
5, pear

The second (fruitchoices) table stores the user's selections:

uid, fid

1, 1
1, 3

I need a query that will produce the entire list of fruit along with a
user's choices. For example:

fid, fruit, uid

1, apple, 1
2, banana, null
3, orange, 1
4, peach, null
5, pear, null

Where 'null' is returned for unselected fruits.

The following query will produce such a list, but only when a uid is not
specified:

SELECT f.*, fc.fruit AS fruitchoice
FROM fruit f
LEFT JOIN fruitchoices fc ON fc.fid = f.fid

Adding a where clause will limit the list to only those chosen fruits:

WHERE fc.uid = 1

Output:

1, apple, 1
3, orange, 1

Can someone show me how to output everything while specifying a uid?

Thanks.



Reply With Quote
  #2 (permalink)  
Old 11-19-2005
Gordon Burditt
 
Posts: n/a
Default Re: (simple?) query dilemma--help please

>I have 2 tables: fruit, fruitchoices.
>
>The first (fruit) is a read-only lookup table:
>
>fid, fruit
>
>1, apple
>2, banana
>3, orange
>4, peach
>5, pear
>
>The second (fruitchoices) table stores the user's selections:
>
>uid, fid
>
>1, 1
>1, 3
>
>I need a query that will produce the entire list of fruit along with a
>user's choices. For example:
>
>fid, fruit, uid
>
>1, apple, 1
>2, banana, null
>3, orange, 1
>4, peach, null
>5, pear, null
>
>Where 'null' is returned for unselected fruits.
>
>The following query will produce such a list, but only when a uid is not
>specified:
>
>SELECT f.*, fc.fruit AS fruitchoice
>FROM fruit f
>LEFT JOIN fruitchoices fc ON fc.fid = f.fid


SELECT f.*, fc.fruit AS fruitchoice
FROM fruit f
LEFT JOIN fruitchoices fc ON fc.fid = f.fid AND fc.uid = 1;

Gordon L. Burditt
Reply With Quote
  #3 (permalink)  
Old 11-19-2005
Bosconian
 
Posts: n/a
Default Re: (simple?) query dilemma--help please

ditt" <gordonb.hazzt@burditt.org> wrote in message
news:11nt0b311odu734@corp.supernews.com...
> >I have 2 tables: fruit, fruitchoices.
> >
> >The first (fruit) is a read-only lookup table:
> >
> >fid, fruit
> >
> >1, apple
> >2, banana
> >3, orange
> >4, peach
> >5, pear
> >
> >The second (fruitchoices) table stores the user's selections:
> >
> >uid, fid
> >
> >1, 1
> >1, 3
> >
> >I need a query that will produce the entire list of fruit along with a
> >user's choices. For example:
> >
> >fid, fruit, uid
> >
> >1, apple, 1
> >2, banana, null
> >3, orange, 1
> >4, peach, null
> >5, pear, null
> >
> >Where 'null' is returned for unselected fruits.
> >
> >The following query will produce such a list, but only when a uid is not
> >specified:
> >
> >SELECT f.*, fc.fruit AS fruitchoice
> >FROM fruit f
> >LEFT JOIN fruitchoices fc ON fc.fid = f.fid

>
> SELECT f.*, fc.fruit AS fruitchoice
> FROM fruit f
> LEFT JOIN fruitchoices fc ON fc.fid = f.fid AND fc.uid = 1;
>
> Gordon L. Burditt


Gordon,

That was too simple (as suspected.)

I was close, but didn't think to add the uid condition to the JOIN clause.

Thanks for the quick response.


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 Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 04:25 AM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0