0

我有以下查询:

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'
4

1 回答 1

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'
于 2012-08-28T20:03:50.920 回答