tags:

views:

89

answers:

3

I have a function that exports a table to CSV and in the query I set which fields will export.

Here is the query:

SELECT lname, fname, email, address1, address2, city, 
state, zip, venue_id, dtelephone, etelephone, tshirt FROM volunteers_2009

The field venue_id is the the id of the venue which is referred to in another table (venues)

So volunteers_2009.venue_id = venues.id

When I open the CSV file it displays the venue_id which I understand, but I need help modifying the query to put in the name of the venue (venues.venue_name) within the CSV file.

Any help is appreciated.

A: 

Standard SQL query for this is (assuming you want both ID and name for the venue):

SELECT a.lname as lname, a.fname as fname, a.email as email,
    a.address1 as address1, a.address2 as address2, a.city as city, 
    a.state as state, a.zip as zip, a.venue_id as venue_id,
    b.venue_name as venue_name, a.dtelephone as dtelephone,
    a.etelephone as etelephone, a.tshirt as tshirt
FROM volunteers_2009 a, venues b
WHERE a.venue_id = b.id
AND a.venue_id IS NOT NULL
UNION ALL
SELECT a.lname as lname, a.fname as fname, a.email as email,
    a.address1 as address1, a.address2 as address2, a.city as city, 
    a.state as state, a.zip as zip, a.venue_id as venue_id,
    '' as venue_name, a.dtelephone as dtelephone,
    a.etelephone as etelephone, a.tshirt as tshirt
FROM volunteers_2009 a
WHERE a.venue_id IS NULL
paxdiablo
Works well, except it does not export the records that do not have a venue_id in that field.
Brad
Are you saying they could be NULL in volunteers_2009; or are you saying they can be set to a value that doesn't exist in venues?
paxdiablo
Avoid the FROM A,B syntax. Use FROM A INNER JOIN B instead.
Joel Coehoorn
It is marked as NULL if there are no venue_id's stored within that field.
Brad
@Brad: Use outer join, thats totally requirement specific
Nrj
Fixed with UNION.
paxdiablo
@Joel, why do you prefer inner join? There's no performance difference in any DB I use.
paxdiablo
It's not the performance. It's the clarity and portability, especially as you start to work with more than just 2 or 3 tables in the same query.
Joel Coehoorn
Ah, I see. I'm guessing it's because we come from different backgrounds. Mainframers have hordes of DBAs to sort out these issues and performance is always rated above readability (so those same DBAs keep their jobs). :-)
paxdiablo
A: 

SELECT lname, fname, email, address1, address2, city, state, zip, b.venue_name, dtelephone, etelephone, tshirt FROM volunteers_2009 a, venues b where a.venue_id = b. venue_id

use proper alias in case both tables have any columns with the same name.

--- EDIT to have all the rows from venues use outer join as

SELECT lname, fname, email, address1, address2, city, state, zip, b.venue_name, dtelephone, etelephone, tshirt FROM volunteers_2009 a(+), venues b where a.venue_id = b. venue_id

Nrj
+1  A: 
SELECT a.lname, a.fname,a. email, a.address1,a. address2, a.city, 
    a.state, a.zip, a.venue_id, a.dtelephone, a.etelephone, a.tshirt,
    COALESCE(b.venue_name,'') AS VenueName
FROM volunteers_2009 a
LEFT JOIN venues b ON b.id=a.venue_id
Joel Coehoorn
@Joel, this has performance implications since you're doing a table lookup and function call for every record. The two pass approach (union all) only does a table lookup for the non-NULL venue IDs and not even that for the NULL ones (and no function calls).
paxdiablo
Although performance probably won't matter so much since you're using MySQL anyway :-).
paxdiablo
Actually, I'll clarify that last statement - I didn't mean MySQL was a toy DB (it is, compared to DB2/z, but that's another issue), just that it's used mostly in places where you don't pay for CPU usage (unlike mainframe DB2).
paxdiablo