我正在尝试将数据导出到csv文件中,但是我的前导零被切断...我无法在拉出数据后修改数据,因此LPAD不是一个选项。此外,ID不是一个设定长度...它可能是6个字符长,或者最多11个字符。它也可能有字母(在这种情况下,csv将这些值显示为字符串。无论如何,带有前导零的字段具有在数据库中前导零并存储为varchar。我需要提取这些值并在零中用csv显示它们:
SELECT
'ID',
'Par 1',
'Par 2'
UNION
(SELECT
result.id as 'ID',
COALESCE(sum(if(result.parameter = '10001', result.result, NULL)),'-') as 'Par 1',
COALESCE(sum(if(result.parameter = '10002', result.result, NULL)),'-') as 'Par 2'
INTO OUTFILE '/tmp/file.csv' FIELDS TERMINATED BY ',' ENCLOSED BY '\"'
FROM (
SELECT
event.id,
resultset.parameter_cd,
resultset.result
FROM event
Inner Join resultset ON resultset.id = event.id
) as result
group by result.id
order by
result.id ASC )
在这种情况下'id'可能有前导零... mysql的结果集显示它们,但是在导出的csv中它们被完全剥离了????
答案 0 :(得分:0)
作为一种解决方法,使用双引号括起id并将enclosed by
取出,因为数字字段不需要它们
(SELECT
concat ('\"', result.id, '\"') as 'ID',
COALESCE(sum(if(result.parameter = '10001', result.result, NULL)),'-') as 'Par 1',
COALESCE(sum(if(result.parameter = '10002', result.result, NULL)),'-') as 'Par 2'
INTO OUTFILE '/tmp/file.csv' FIELDS TERMINATED BY ','