4

我正在尝试将数据导出到 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中它们被完全去除????

4

1 回答 1

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 ','    
于 2012-10-18T16:54:39.237 回答