Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7973789
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T08:12:18+00:00 2026-06-04T08:12:18+00:00

My case is as follow: I have a table articles with these details: article

  • 0

My case is as follow:

I have a table articles with these details:

    article | image1 | image2 | image3 
    ---------------------------------    
      1     | im-x
      2     | im-y
      3     | im-z 

and other article_images table with this data:

    article | image    
    ---------------    
        1   | im-a
        1   | im-b
        2   | im-c
        2   | im-d
        3   | im-e 

I need to update the table articles like this:

    article | image1 | image2 | image3    
    ---------------------------------    
       1    | im-x   | im-a   | im-b
       2    | im-y   | im-c   | im-d    
       3    | im-z   | im-e   | 

I know it seems not very difficult, but impossible to find an example on google. Can anyone help me, please?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T08:12:19+00:00Added an answer on June 4, 2026 at 8:12 am

    This query will produce the desired Pivot:

    SELECT a.article, coalesce(i1.image,'') AS img1, coalesce(i2.image, '') AS img2
      FROM (SELECT article FROM article_images GROUP BY article) a
      LEFT JOIN article_images i1 ON a.article = i1.article AND i1.image =
           (SELECT image FROM article_images WHERE article = a.article
             ORDER BY image LIMIT 1)
      LEFT JOIN article_images i2 ON a.article = i2.article AND i2.image =
           (SELECT image FROM article_images WHERE article = a.article
             ORDER BY image LIMIT 1 OFFSET 1);
    

    A bit complicated, but this subquery will work in MySQL + PostgreSQL.

    To update use (this UPDATE is MySQL specific):

    UPDATE articles a, (SELECT a.article, coalesce(i1.image, '') AS img1,
                        coalesce(i2.image, '') AS img2
      FROM (SELECT article FROM article_images GROUP BY article) a
      LEFT JOIN article_images i1 ON a.article = i1.article AND i1.image =
           (SELECT image FROM article_images WHERE article = a.article
             ORDER BY image LIMIT 1)
      LEFT JOIN article_images i2 ON a.article = i2.article AND i2.image =
           (SELECT image FROM article_images WHERE article = a.article
             ORDER BY image LIMIT 1 OFFSET 1)) AS s
    SET a.image2 = s.img1, a.image3 = s.img2
    WHERE a.article = s.article;
    

    If your database support window functions + CTE, like SQL Server, PostgreSQL or ORACLE, the following query can be used instead to generate Pivot:

    WITH rowed AS (
      SELECT article, image,
             row_number() OVER (PARTITION BY article ORDER BY image) AS row
        FROM article_images)
    SELECT a.article, coalesce(i1.image, '') AS img1, coalesce(i2.image, '') AS img2
      FROM (SELECT article FROM article_images GROUP BY article) AS a
      LEFT JOIN rowed i1 ON i1.article = a.article AND i1.row = 1
      LEFT JOIN rowed i2 ON i2.article = a.article AND i2.row = 2;
    

    Now you have a single row per article in a shorter way and you can use this subquery to update:

    UPDATE articles a
    SET image2 = s.img1,
        image3 = s.img2
    FROM (WITH rowed AS (
      SELECT article, image,
             row_number() OVER (PARTITION BY article ORDER BY image) AS row
        FROM article_images)
      SELECT a.article, coalesce(i1.image, '') AS img1, coalesce(i2.image, '') AS img2
         FROM (SELECT article FROM article_images GROUP BY article) AS a
          LEFT JOIN rowed i1 ON i1.article = a.article AND i1.ROW = 1
          LEFT JOIN rowed i2 ON i2.article = a.article AND i2.ROW = 2) AS s
    WHERE a.article = s.article;
    

    This UPDATE query will work in PostgreSQL, but likely it wont on ORACLE / SQL Server.

    You can play around with MySQL and PostgreSQL variants.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table say, ITEM, in MySQL that stores data as follows: ID
My use case is as follows -- I have a database table with around
I have a minimal case as follows:- Table Posts PostID - PK PostDateTime Table
This is a follow-up question to my previous one . Situation: Table 1: +--------------------+--------------------+
I have 2 tables: a main APPLICATION table that holds the core data, and
I have data spread over two MySQL tables with different structures. One table has
Situation is this: I have a table for staff and another one for course.
I have two tables. The first table holds simple user data and has the
My case is as follows : I have a bunch of functions and declarations
Case example: I have a long list of items, and when I put my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.