将多行mysql字符串转换为预定义的csv格式?

时间:2013-09-11 06:43:40

标签: mysql database excel csv converter

这更像是关于csv,excel,mysql文件的操作方法问题而不是标准编码问题。不过我真的很感激这方面的帮助。

我有这种预定义的CSV格式,我需要将其导入到使用Sequel Pro的数据库。

ID,"user_login","user_pass","user_nicename","user_email","user_url","user_registered","user_activation_key",user_status,"display_name"

1,"some_name","$P$BDMmGci3KAqNQwfPtgzfQTp8D3k7bC.","some_name","some.name@gmx.net","","2013-06-27 11:32:08","",0,"Some Name"

另一方面,我有一个看起来像这样的旧数据库文件......

CREATE TABLE `ezxsubscription` (                                
  `id` int(11) NOT NULL auto_increment,                         
  `version_status` int(11) NOT NULL default '0',                            
  `subscriptionlist_id` int(11) default '0',                            
  `email` varchar(255) default '',                              
  `hash` varchar(255) default '',                               
  `status` int(11) default '0',                             
  `vip` int(11) default '0',                                
  `last_active` int(11) NOT NULL default '0',                           
  `output_format` varchar(255) default '',                          
  `creator_id` int(11) NOT NULL default '0',                            
  `created` int(11) NOT NULL default '0',                           
  `confirmed` int(11) NOT NULL default '0',                         
  `approved` int(11) NOT NULL default '0',                          
  `removed` int(11) NOT NULL default '0',                           
  `user_id` int(11) default '0',                                
  `bounce_count` int(11) default '0',                               
  PRIMARY KEY  (`id`,`version_status`)                          
) TYPE=MyISAM;                                  

--                                  
-- Dumping data for table `ezxsubscription`                         
--                                  

INSERT INTO `ezxsubscription` VALUES (454,1,5,'name@surname.com','31b6bde64e1282ba82d7d0c8ad6ebaa9',2,0,0,'2',1127,1142840848,0,1142840868,0,2633,0);   
INSERT INTO `ezxsubscription` VALUES (1,1,5,'name.name@gmail.com','fff4b8d75ecdfad43ed8c89444939cfb',2,0,0,'2',10,1141956489,1141956489,1141956489,0,14,0);
…

数据库中有400个用户,我无法手动排序。我需要让数据库文件采用上面预定义CSV的格式。电子邮件地址应为user_loginuser_name。剩下的旧数据库如approvedremoved或其他任何东西都可以忽略不计。 这只是关于用户登录和密码。

关于如何这样做的任何聪明的想法?目的是将旧数据库的所有旧用户导入新数据库(基于Wordpress)。

我真的很感激一些提示或技巧。

提前谢谢你。 马特


更新

这是ezxsubscription表的旧格式。

"id","version_status","subscriptionlist_id","email","hash","status","vip","last_active","output_format","creator_id","created","confirmed","approved","removed","user_id","bounce_count"
1,1,5,"andreas@gmail.at","fff4b8d75ecdfad43ed8c89444939cfb",2,0,0,2,10,1141956489,1141956489,1141956489,0,14,0

我现在要做的就是将这些用户转换为新的wordpress格式:

"ID","user_login","user_pass","user_nicename","user_email","user_url","user_registered","user_activation_key","user_status","display_name"
1,"local","$P$BCf7QHr3rsp0YNZd7eMfYgncRZmR6j0","local","temp@local.dev","","2013-03-14 18:46:29","",0,"local"

我甚至在理解旧数据库中hash的含义时遇到了问题。 我想做的就是将所有现有的旧用户复制到新的wordpress数据库,这样我就可以获得电子邮件地址和密码,以便他们能够再次登录。

所以我想把旧电子邮件作为新wordpress db的用户名。 hash应该是wordpress中的新user_pass。但是我只希望hash是当前登录的密码 - 我甚至不确定。

亲切的问候, 马特

1 个答案:

答案 0 :(得分:1)

使用SELECT ... INTO OUTFILE语法从旧表中使用类似

的查询创建CSV文件
SELECT *
  INTO OUTFILE '/path/to/your/file.csv'
    FIELDS TERMINATED BY ',' ENCLOSED BY '"'
    LINES TERMINATED BY '\n'
  FROM
(
  SELECT 'id', 'user_login', 'user_pass', 'user_nicename','user_email','user_url','user_registered','user_activation_key','user_status','display_name'
  UNION ALL
  SELECT id, email, hash, SUBSTRING_INDEX(email, '@', 1), email, '', created, '', status, SUBSTRING_INDEX(email, '@', 1)
    FROM ezxsubscription
) q

创建文件的内容:

"id","user_login","user_pass","user_nicename","user_email","user_url","user_registered","user_activation_key","user_status","display_name"
"454","name@surname.com","31b6bde64e1282ba82d7d0c8ad6ebaa9","name","name@surname.com","","1142840848","","2","name"
"1","name.name@gmail.com","fff4b8d75ecdfad43ed8c89444939cfb","name.name","name.name@gmail.com","","1141956489","","2","name.name"