我有一个Mysql错误->错误->操作数应包含1列

问题描述 投票:0回答:1

((我的英语不好,很抱歉)我想将两个不同的表插入一个。“我要插入的表”The tables I want to insert

我已经尝试了所有不同的方式。

INSERT INTO com_short 
    (category_id, media_folder_id, gallery_folder_id, status, important, 
    bgPositioning, published_at, published_by, created_at, modified_at,title, 
    slug, body, featured_image_file_id) 

SELECT (SELECT com_post.id, category_id, media_folder_id, gallery_folder_id,    
               status, important, bgPositioning, published_at, published_by, 
               com_post.created_at, com_post.modified_at 
        FROM com_post 
            INNER JOIN com_post_translation ON com_post.id = com_post_translation.post_id 
        WHERE com_post_translation.post_id = com_post.id
        ),

        (SELECT title, slug, body,featured_image_file_id 
        FROM com_post_translation 
        WHERE com_post.id = com_post_translation.post_id
        );

但显示错误:The error

mysql sql
1个回答
0
投票

错误原因:您不能在子查询中选择多个列:

SELECT (SELECT com_post.id, category_id, media_folder_id, gallery_folder_id,    
               status, important, bgPositioning, published_at, published_by, 
               com_post.created_at, com_post.modified_at 
        FROM com_post 
            INNER JOIN com_post_translation ON com_post.id = com_post_translation.post_id 
        WHERE com_post_translation.post_id = com_post.id
        ),

        (SELECT title, slug, body,featured_image_file_id 
        FROM com_post_translation 
        WHERE com_post.id = com_post_translation.post_id
        );

简单说明

这将产生错误:

select (select '1', '2');

这不会:

select (select '1');

Check the demo

将是什么[[无错误的方法] ::INSERT INTO com_short (category_id --1 , media_folder_id --2 , gallery_folder_id --3 , status --4 , important --5 , bgPositioning --6 , published_at --7 , published_by --8 , created_at --9 , modified_at --10 , title --11 , slug --12 , body --13 , featured_image_file_id) --14 SELECT cpt.category_id --1 , cpt.media_folder_id --2 , cpt.gallery_folder_id --3 , cpt.status --4 , cpt.important --5 , cpt.bgPositioning --6 , cpt.published_at --7 , cpt.published_by --8 , com_post.created_at --9 , com_post.modified_at --10 , cpt.title --11 , cpt.slug --12 , cpt.body --13 , cpt.featured_image_file_id --14 FROM com_post INNER JOIN com_post_translation cpt ON com_post.id = cpt .post_id

:这只是一个例子,从您的问题中我无法确定您想要做什么。...

其他信息

  • cpt是表com_post_translation的别名(它使您的代码“更轻”且更易于阅读)。

  • 当您使用ON关键字连接两个表时,无需在where子句中重复该条件。
© www.soinside.com 2019 - 2024. All rights reserved.