1

我有一个将表导出到 CSV 的函数,并在查询中设置将导出哪些字段。

这是查询:

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

字段venue_id是在另一个表(venues)中引用的场地的id

所以志愿者_2009.venue_id =venues.id

当我打开 CSV 文件时,它会显示我理解的场地 ID,但我需要帮助修改查询以将场地名称 (venues.venue_name) 放入 CSV 文件中。

任何帮助表示赞赏。

4

3 回答 3

1
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
于 2008-11-25T03:54:39.877 回答
0

对此的标准 SQL 查询是(假设您需要场地的 ID 和名称):

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
于 2008-11-25T03:37:45.403 回答
0
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

如果两个表都有任何同名的列,请使用正确的别名。

--- 编辑让场地中的所有行都使用外连接作为

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
于 2008-11-25T03:43:42.910 回答