doufu7464 2016-09-19 19:25
浏览 17
已采纳

将查询结果插入同一个表中

(For WordPress folks: I am creating a set of new "custom field"s called sidebar_buy_now by combining pairs of existing "custom field"s.)

I have a table: wp_postmeta with 4 fields: meta_id,post_id,meta_key,meta_value.

meta_idis the primary key and should auto-increment.

I have a working SELECT query which produces the desired result for the last 3 fields: post_id,meta_key,meta_value:

SELECT L.`post_id`, "sidebar_buy_now" AS "meta_key",
CONCAT(L.`meta_value`, ' | ',R.`meta_value`) 
AS "meta_value" FROM `wp_postmeta` L INNER JOIN `wp_postmeta` R
ON L.`post_id` = R.`post_id`
AND L.`meta_key` = 'sidebar_buy_now_url' 
WHERE R.`meta_key` = 'sidebar_buy_now_title'

I want to insert the results of this query into the wp_postmeta table while auto-incrementing meta_id.

This is the INSERT query I am trying which does not work:

INSERT into `wp_postmeta` (`post_id`,`meta_key`,`meta_value`) 
SELECT L.`post_id`, "sidebar_buy_now" AS "meta_key", 
CONCAT(L.`meta_value`, ' | ',R.`meta_value`) AS "meta_value" FROM `wp_postmeta` L INNER JOIN `wp_postmeta` R ON L.`post_id` = R.`post_id`
AND L.`meta_key` = 'sidebar_buy_now_url' WHERE R.`meta_key` = sidebar_buy_now_title'

Getting this error:

#1136 - Column count doesn't match value count at row 1.

Perhaps because I don't know how to specify an auto-incrementing meta-id in the SELECT... (?) Anyone know how to fix this?

  • 写回答

1条回答 默认 最新

  • du5591 2016-09-19 19:31
    关注
    INSERT into wp_postmeta (post_id,meta_key,meta_value)
    SELECT
       L.post_id
       ,"sidebar_buy_now" AS "meta_key"
       ,CONCAT(L.meta_value, ' | ',R.meta_value) AS "meta_value"
    FROM
       wp_postmeta L
       INNER JOIN wp_postmeta R
       ON L.post_id = R.post_id AND L.meta_key = 'sidebar_buy_now_url'
    WHERE R.meta_key = 'sidebar_buy_now_title'
    

    Because meta_id is an auto increment column you don't actually insert anything into you. You simply omit it from the insert statement.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能