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
|