2017-03-04 65 views
0

我怎樣才能從下面wp_usermeta將數據插入到公司表當wp_usermeta有3列中的所有數據:插入數據

wp_usermeta的結構是:ID - USER_ID - meta_key - meta_value

Below user_id + meta_key + meta_value must insert into table company

meta_key = 'gender' -> gender 
meta_key = 'first_name' -> first_name 
meta_key = 'last_name' -> last_name 
meta_key = 'street_address' -> address 
meta_key = 'zipcode' -> zipcode 
meta_key = 'city' -> city 
meta_key = 'country' -> country 
meta_key = 'telephone' -> company_phone 

不知道如何解決這個問題。

CREATE TABLE IF NOT EXISTS `wp_usermeta` (
    `umeta_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, 
    `user_id` bigint(20) unsigned NOT NULL DEFAULT '0', 
    `meta_key` varchar(255) DEFAULT NULL, 
    `meta_value` longtext, 
    PRIMARY KEY (`umeta_id`), 
    KEY `user_id` (`user_id`), 
    KEY `meta_key` (`meta_key`(191)) 
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=6026 ; 

CREATE TABLE IF NOT EXISTS `wp_agent_companion` (
    `id` int(11) NOT NULL AUTO_INCREMENT, 
    `user_id` int(15) NOT NULL, 
    `commerce_number` varchar(32) DEFAULT NULL, 
    `vat_number` varchar(32) DEFAULT NULL, 
    `company_name` varchar(64) DEFAULT NULL, 
    `gender` char(1) DEFAULT NULL, 
    `first_name` varchar(12) NOT NULL, 
    `last_name` varchar(34) NOT NULL, 
    `service` varchar(64) DEFAULT NULL, 
    `address` varchar(64) DEFAULT NULL, 
    `zipcode` varchar(12) DEFAULT NULL, 
    `city` varchar(64) DEFAULT NULL, 
    `country` varchar(64) DEFAULT NULL, 
    `company_phone` varchar(24) DEFAULT NULL 
    PRIMARY KEY (`id`), 
    UNIQUE KEY `id` (`id`) 
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=93 ; 
+1

您可以分享'wp_usermeta'和'company'表的定義嗎? – Mureinik

+0

我已將它添加到線程中,請參閱上文。 – Hermants

回答

1

請問這個大的查詢是否會讓您的表的臨時表?

INSERT INTO wp_agent_companion 
(user_id, gender, 
first_name, last_name, address, zipcode, 
city, country, company_phone) 
SELECT 
    t1.user_id as user_id, 
    t1.meta_value as gender, 
    t2.meta_value as first_name, 
    t3.meta_value as last_name, 
    t4.meta_value as address, 
    t5.meta_value as zipcode, 
    t6.meta_value as city, 
    t7.meta_value as country, 
    t8.meta_value as company_phone 
FROM 
    (SELECT * FROM wp_usermeta) as t1 
LEFT JOIN (SELECT * FROM wp_usermeta) as t2 
ON t2.user_id = t1.user_id 
LEFT JOIN (SELECT * FROM wp_usermeta) as t3 
ON t3.user_id = t1.user_id 
LEFT JOIN (SELECT * FROM wp_usermeta) as t4 
ON t4.user_id = t1.user_id 
LEFT JOIN (SELECT * FROM wp_usermeta) as t5 
ON t5.user_id = t1.user_id 
LEFT JOIN (SELECT * FROM wp_usermeta) as t6 
ON t6.user_id = t1.user_id 
LEFT JOIN (SELECT * FROM wp_usermeta) as t7 
ON t7.user_id = t1.user_id 
LEFT JOIN (SELECT * FROM wp_usermeta) as t8 
ON t8.user_id = t1.user_id 
WHERE 
    t1.meta_key = 'gender' AND 
    t2.meta_key = 'first_name' AND 
    t3.meta_key = 'last_name' AND 
    t4.meta_key = 'street_address' AND 
    t5.meta_key = 'zipcode' AND 
    t6.meta_key = 'city' AND 
    t7.meta_key = 'country' AND 
    t8.meta_key = 'telephone' 

只要你能夠將它們插入到companyINSERT INTO SELECT (..),將你知道。

INSERT [LOW_PRIORITY | HIGH_PRIORITY] [IGNORE] 
    [INTO] tbl_name 
    [PARTITION (partition_name,...)] 
    [(col_name,...)] 
    SELECT ... 
    [ ON DUPLICATE KEY UPDATE col_name=expr, ... ] 

About INSERT SELECT

+0

是的,謝謝。很快。 – Hermants

+0

我得到的所有數據的列表,但不明白插入到select() – Hermants

+0

看一下在答案中更新INSERT查詢@Hermants – MohaMad