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

  • Home
  • SEARCH
  • 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 9255801
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T11:47:30+00:00 2026-06-18T11:47:30+00:00

I have a table: CREATE TABLE `Issues` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT,

  • 0

I have a table:

CREATE TABLE `Issues` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `title` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB;

I have another table:

CREATE TABLE `Attachments` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `issue_id` int(11) DEFAULT NULL,
  `attachment` text,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB;

How can I get the data to look like this:

issue_id    title    attachment1    attachment2    attachment3
--------------------------------------------------------------
1           T1       a1.png         a2.png
2           T2
3           T3       b4.gif         xyz.doc        ttt.file

The problem I can’t figure out is how to get the dynamic set of attachments into a dynamic column grouped by issue. I have determined that the maximum amount of attachments for one issue is 12, but the total per ticket can be anywhere from 0-12. I’m stumped…

I’ve tried this
MySQL pivot row into dynamic number of columns, but can’t make sense of it in my case because I’m building dynamic columns based on total matches per record…

Any help would be greatly appreciate. Please let me know if this doesn’t make sense.

Nino

  • 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-18T11:47:31+00:00Added an answer on June 18, 2026 at 11:47 am

    Here is one way to accomplish this if you know the max number is 12. It uses MAX and CASE, getting the Row Number for each attachment.

    SELECT 
        I.Id issue_id, 
        I.title, 
        MAX(CASE WHEN d.row_number = 1 THEN D.attachment END) attachment1,
        MAX(CASE WHEN d.row_number = 2 THEN D.attachment END) attachment2,
        MAX(CASE WHEN d.row_number = 3 THEN D.attachment END) attachment3,
        MAX(CASE WHEN d.row_number = 4 THEN D.attachment END) attachment4,
        MAX(CASE WHEN d.row_number = 5 THEN D.attachment END) attachment5,
        MAX(CASE WHEN d.row_number = 6 THEN D.attachment END) attachment6,
        MAX(CASE WHEN d.row_number = 7 THEN D.attachment END) attachment7,
        MAX(CASE WHEN d.row_number = 8 THEN D.attachment END) attachment8,
        MAX(CASE WHEN d.row_number = 9 THEN D.attachment END) attachment9,
        MAX(CASE WHEN d.row_number = 10 THEN D.attachment END) attachment10,
        MAX(CASE WHEN d.row_number = 11 THEN D.attachment END) attachment11,
        MAX(CASE WHEN d.row_number = 12 THEN D.attachment END) attachment12
    FROM Issues I
      LEFT JOIN (
      SELECT 
          a.issue_id, 
          @running:=if(@previous=a.issue_id,@running,0) + 1 as row_number,
          @previous:=a.issue_id,
          a.attachment
      FROM Attachments a
          JOIN    (SELECT @previous := 0) r
        ORDER BY a.issue_id, a.attachment
      ) D ON I.ID = D.issue_id
    GROUP BY I.Id, I.Title
    

    And here is the SQL Fiddle.

    I had to make an edit to make the rownumber reset per group. Should be working now. Also, per @spencer7593’s great comment, I’ve updated the query slightly.

    –EDIT–

    In response to OP’s comment about needing dynamic results, this should work:

    SET @sql = NULL;

    SELECT
      GROUP_CONCAT(DISTINCT
        CONCAT(
          'MAX(IF(d.row_number = ', d.row_number, ',D.attachment,NULL)) AS attachment', d.row_number)
      ) INTO @sql
    FROM Issues I
      LEFT JOIN (
      SELECT 
          a.issue_id, 
          @running:=if(@previous=a.issue_id,@running,0) + 1 as row_number,
          @previous:=a.issue_id,
          a.attachment
      FROM Attachments a
          JOIN    (SELECT @previous := 0) r
        ORDER BY a.issue_id, a.attachment
      ) D ON I.ID = D.issue_id
    ;
    
    SET @sql = CONCAT('SELECT I.Id issue_id, 
                              I.title, ', @sql, ' 
                      FROM Issues I
                      LEFT JOIN (
                      SELECT 
                          a.issue_id, 
                          @running:=if(@previous=a.issue_id,@running,0) + 1 as row_number,
                          @previous:=a.issue_id,
                          a.attachment
                      FROM Attachments a
                          JOIN    (SELECT @previous := 0) r
                        ORDER BY a.issue_id, a.attachment
                      ) D ON I.ID = D.issue_id
                    GROUP BY I.Id, I.Title');
    
    PREPARE stmt FROM @sql;
    EXECUTE stmt;
    DEALLOCATE PREPARE stmt;
    

    And here is the SQL Fiddle.

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

Sidebar

Related Questions

I have this table CREATE TABLE `codes` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
I have a table: CREATE TABLE siteConfig ( settingName VARCHAR(64) NOT NULL, value VARCHAR(255),
I have this table: CREATE TABLE `test` ( `ID` int(11) NOT NULL auto_increment, `text`
I have this table: CREATE TABLE `point` ( `id` INT(11) NOT NULL AUTO_INCREMENT, `siteid`
i have this table create table eveniment( + evenimentId bigint not null auto_increment primary
I have this table CREATE TABLE [dbo].[friend_blocked_list]( [subdomain] [varchar](50) NOT NULL, [un] [nvarchar](50) NOT
Assume I've got the table: CREATE TABLE test ( ID INT AUTO_INCREMENT PRIMARY KEY,
I have the following table: CREATE TABLE [dbo].[omgbbq]( [tbl_key] [int] IDENTITY(1,1) NOT NULL, [name]
I have a table similar to CREATE TABLE `mytable` ( `ID` int(11) unsigned NOT
I have a table CREATE TABLE `messages` ( `uid` BIGINT NOT NULL , `mid`

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.