List:General Discussion« Previous MessageNext Message »
From:Hans van Harten Date:December 30 2003 12:07am
Subject:Re: LEFT JOIN help (or "come and slap the newbie")
View as plain text  
Dan Hansen wrote:
> is essentially giving me what I need:
> 
> CREATE TEMPORARY TABLE temptable
> SELECT state.name AS state , group.name AS group,
> group.zip AS zip, city.name AS city
> FROM city, group, zip
> LEFT JOIN state ON city.state_id = state.id
> WHERE group.zip = zip.zip
> AND zip.city_id = city.id;
> 
> INSERT INTO temptable
> SELECT name, NULL, -1, NULL FROM state;
> 
> SELECT state, group, zip, city
> FROM temptable
> ORDER BY state, zip, group ;
> 
> (Once plugged into PHP code, I will replace "temptable" with a unique
> code-generated string).
> 
Should run in a single query too...

SELECT state.name AS state , group.name AS group,
group.zip AS zip, city.name AS city
FROM city, group, zip
LEFT JOIN state ON city.state_id = state.id
WHERE group.zip = zip.zip
AND zip.city_id = city.id

UNION
SELECT name, NULL, -1, NULL FROM state

ORDER BY state, zip, group ;

.. although untested. -do remove the semicolon twice-

Hans


Thread
LEFT JOIN help (or "come and slap the newbie")D. R. Hansen14 Oct
  • Re: LEFT JOIN help (or "come and slap the newbie")Diana Soares15 Oct
    • Re: LEFT JOIN help (or "come and slap the newbie")D. R. Hansen15 Oct
      • Re: LEFT JOIN help (or "come and slap the newbie")Roger Baklund15 Oct
    • Re: LEFT JOIN help (or "come and slap the newbie")Dan Hansen16 Oct
  • Re: LEFT JOIN help (or "come and slap the newbie")Hans van Harten30 Dec