需要一个mysql查询来从mm表单中提取数据

时间:2010-06-07 09:26:24

标签: php mysql export

http://cl.ly/201a11b430bf60ef9e97

在我清理它们之后,

是行的样子:

SELECT fk_form_joiner_id,form_key,value FROM wp_contactform_submit_data WHERE form_key!='page_post_id' AND form_key!='captcha-802' AND form_key!='page_post_title' AND form_key!='user_ID'

我需要的是一个查询,它会将其转换为如下结构:

id | First-name | Surname-name | your-email | dob | address-1 | address-2 | county | postcode | phone
-------------------------------------------------------------------------------------------------------
88 | jack       | Somethin     | e@ma.il    | dob | (etc)

我想我知道通过php实现这一目标的方法,但我认为必须有一种方法可以将其作为mysql查询。

最终,我需要导出这个(进入csv),但如果你能给我一个查询,将其输出到一个mysql表(或至少一个可行的选择查询,我可以转为插入查询循环,创建一个表),我会在那里形成。

谢谢。

1 个答案:

答案 0 :(得分:0)

SELECT DISTINCT(fk_form_joiner_id) AS jid, (SELECT value FROM wp_contactform_submit_data WHERE fk_form_joiner_id = jid AND form_key = 'First-name') AS first_name, (SELECT value FROM wp_contactform_submit_data WHERE fk_form_joiner_id = jid AND form_key = 'Surname-name') AS last_name, (SELECT value FROM wp_contactform_submit_data WHERE fk_form_joiner_id = jid AND form_key = 'dob') AS dob, (SELECT value FROM wp_contactform_submit_data WHERE fk_form_joiner_id = jid AND form_key = 'address-1') AS address1, (SELECT value FROM wp_contactform_submit_data WHERE fk_form_joiner_id = jid AND form_key = 'address-2') AS address2, (SELECT value FROM wp_contactform_submit_data WHERE fk_form_joiner_id = jid AND form_key = 'town') AS town, (SELECT value FROM wp_contactform_submit_data WHERE fk_form_joiner_id = jid AND form_key = 'county') AS county, (SELECT value FROM wp_contactform_submit_data WHERE fk_form_joiner_id = jid AND form_key = 'postcode') AS postcode, (SELECT value FROM wp_contactform_submit_data WHERE fk_form_joiner_id = jid AND form_key = 'phone') AS phone FROM wp_contactform_submit_data ORDER BY jid INTO OUTFILE '/var/www/vhosts/domain.co.uk/httpdocs/exp/output.csv' FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n' 

BOOM。