0

I want to insert two different tables into one as per the below illustration: The tables I want to insert

I've tried several ways like:

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
        );

But it shows an error as follows:

The error

What am I missing?

Kate Orlova
  • 3,225
  • 5
  • 11
  • 35
John
  • 1
  • 1
    Welcome to SO, have a look at https://stackoverflow.com/questions/14046838/mysql-operand-should-contain-1-columns – indofraiser Feb 19 '20 at 09:37
  • Make it easy to assist you - simplify! [mcve] – jarlh Feb 19 '20 at 09:38
  • 1
    Hi @indofraiser I have seen only now your suggested link. If I have seen it before I would not write my answer.... Now that is already there I will leave it because I believe it has some extra info OP could use... If you do not agree I will delete it.. .Cheers! – VBoka Feb 19 '20 at 10:56

1 Answers1

0

Reason of the error: you can not select more than one column in the subquery:

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
        );

Simple explanation :

This will produce error:

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

This will not:

select (select '1');

Check the demo

What would be the way to do it withot errors:

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

Note: This is just an example, I can not know for sure from your question what do you want to do....

Additional info:

  • cpt is alias for the table com_post_translation (It makes your code "lighter" and easyer to read).

  • When you join two tables with ON keyword there is no need to repeat that condition in the where clause.

VBoka
  • 8,995
  • 3
  • 16
  • 24