多列输出中的不同列值(然后转换为CSV)

时间:2012-08-28 20:00:54

标签: sql

我有以下查询:

SELECT first, last, title, email, org 
FROM people WHERE email <> "" 
INTO OUTFILE 'C:/testfile.csv' FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n'

哪个有效。我需要选择不同的电子邮件(我不希望同一封电子邮件中有多个条目)。

它会起作用吗?:

SELECT first, last, title, distinct(email), org 
FROM people WHERE email <> "" 
INTO OUTFILE 'C:/testfile.csv' FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n'

1 个答案:

答案 0 :(得分:3)

您使用的是哪个SQL平台/版本?通常,这将使用group by语句完成。类似的东西:

SELECT first, last, title, email, org 
FROM people 
GROUP BY Email
WHERE email <> ""
INTO OUTFILE 'C:/testfile.csv' FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n'

以上内容实际上适用于SQL的某些平台/版本,但“正确”(标准sql)方式如下(当然,如果其他字段对于同一电子邮件不同,则会得到未定义的结果):

SELECT max(first), max(last), max(title), email, max(org) 
FROM people 
GROUP BY Email
WHERE email <> ""
INTO OUTFILE 'C:/testfile.csv' FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n'