我有一个MySQL查询,导出为CSV文件。收到CSV的人希望我包含列标题。显然我可以在查询中对它们进行硬编码,但是想知道是否有办法返回一个具有所有已查询列的名称的行?
我当前的查询在下面 - 我希望以编程方式包含列标题,如果我可以以防万一我忘记在更改时更新它。
由于
SET @sql_text =
CONCAT (
"SELECT
cf_id as FoundUsFrom,
firstname as CustomerName1,
lastname as CustomerName2,
address1 as CAddress1,
address2 as CAddress2,
'' as CAddress3,
towncity as CCity,
postcode as CPostCode,
email as 'email',
telephone as HomePhone,
mobile as MobilePhone,
'' as WorkPhone,
chidname as PName,
'' as ToBeAged,
partyaddress1 as PAddress1,
partyaddress2 as PAddress2,
'' as PAddress3,
partytowncity as PCity,
partypostcode as PPostCode,
/*NEED VALIDATION*/
celebrating as PType,
partydate as PDate,
partyboffinstart as PTime,
specialinstructions as SpecialInst,
numberguests as Guests,
IF(airexp = '','N','Y') as Air,
IF(chemcocklngexp = '','N','Y') as CocktailLong,
IF(chemcockshtexp = '','N','Y') as CocktailShort,
IF(chemreact = '','N','Y') as Reactions,
IF(dryicebolton = '','N','Y') as DryIce,
IF(electricitybolton = '','N','Y') as Electricity,
IF(flightbolton = '','N','Y') as Flight,
IF(fourelementsbolton = '','N','Y') as Elements,
IF(magicbolton = '','N','Y') as Magic,
IF(slimebolton = '','N','Y') as Slime,
IF(sweetbolton = '','N','Y') as Sweets,
IF(miniimpsbolton = '','N','Y') as MiniImps,
IF(extravaganzabolton = '','N','Y') as Extravaganza,
IF(dryicecreambolton = '','N','Y') as AIceCream,
IF(rocketbolton = '','N','Y') as ARocket,
IF(foodbreakbolton = '','N','Y') as AFood,
partybags as PBags,
deluxepartybags as PDBags
INTO OUTFILE 'PATHTO/data/bookings",
DATE_FORMAT( NOW(), '%Y%m%d%s'),
".csv' FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"'
LINES TERMINATED BY '\n'
FROM sb_chronoforms_data_bookingformcombined"
);
PREPARE s1 FROM @sql_text;
EXECUTE s1;
DROP PREPARE s1;
答案 0 :(得分:0)
这是一个奇怪的问题:)试试
SELECT
*
FROM
information_schema.COLUMNS
where
table_chema = 'your_schema'
and table_name = 'your_table';
使用 ordinal_position 显示相关列名
答案 1 :(得分:0)
单独记录这个,因为这是解决问题的另一种方法:
从双UNION中选择'col_1','col_2','col_3','col_4'全部选择 来自table_1的col_1,col_2,col_3,col_4;