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 8652515
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T14:19:26+00:00 2026-06-12T14:19:26+00:00

So I have a relatively complicated MySQL query that is giving a Duplicate Column,

  • 0

So I have a relatively complicated MySQL query that is giving a Duplicate Column, for reasons I haven’t been able to determine. I’m somewhat new to SQL.

The query is supposed to join a list of “problem” entries with the database of “tickets,” specifically the latest ticket for the particular id. Thus I have an inner query that executes a JOIN of the ticket table on itself to get the latest ticket. This works correctly. I then attempt to JOIN this inner selection with the problem database, however, I get a Duplicate Column Name id, despite the fact that this is the column name I am joining on. My understanding is that when you join on a particular column, it shouldn’t “count” as duplicate…

Anyway, the query:

SELECT * FROM `problem` P
LEFT JOIN (
    SELECT * FROM (
        SELECT id, MAX(ticket_id) AS ticket_id
        FROM `ticket`
        WHERE ticket_status = 'o'
        GROUP BY id
    ) AS MAX
    INNER JOIN (
        SELECT id, ticket_id, other_data, time_stamp FROM `ticket`
    ) CUR ON MAX.id = CUR.id AND MAX.ticket_id = CUR.ticket_id
) T ON P.id=T.id
WHERE
        P.since IS NOT NULL;

As I said, if I do just this:

SELECT * FROM (
    SELECT id, MAX(ticket_id) AS ticket_id
    FROM `ticket`
    WHERE ticket_status = 'o'
    GROUP BY id
) AS MAX
INNER JOIN (
    SELECT id, ticket_id, other_data, time_stamp FROM `ticket`
) CUR ON MAX.id = CUR.id AND MAX.ticket_id = CUR.ticket_id

I get a table of the latest ticket for each id.

One thing I have tried to fix this is to SELECT MAX.* FROM ( instead of just SELECT * FROM (. This eliminates the duplicate column name error, but the inner query no longer returns any other_data or time_stamp – I only get the id and ticket_id columns. This defeats the purpose of the join in the first place, of course.

  • 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-12T14:19:27+00:00Added an answer on June 12, 2026 at 2:19 pm

    Alias the id column as below:

    SELECT * FROM `problem` P
    LEFT JOIN (
        SELECT * FROM (
            SELECT id AS max_id, MAX(ticket_id) AS ticket_id
            FROM `ticket`
            WHERE ticket_status = 'o'
            GROUP BY max_id
        ) AS MAX
        INNER JOIN (
            SELECT id AS cur_id, ticket_id, other_data, time_stamp FROM `ticket`
        ) CUR ON MAX.max_id = CUR.cur_id AND MAX.ticket_id = CUR.ticket_id
    ) T ON P.id=T.max_id
    WHERE
            P.since IS NOT NULL;
    

    I wrote in a comment that you could just select the needed columns to avoid the duplicate and the question author understood I meant the external select, I was talking about the inner one. Hope I wrote it right, didn’t test.

    SELECT * FROM `problem` P
    LEFT JOIN (
        SELECT CUR.id, CUR.ticket_id, CUR.other_data, CUR.time_stamp FROM (
            SELECT id, MAX(ticket_id)
            FROM `ticket`
            WHERE ticket_status = 'o'
            GROUP BY max_id
        ) AS MAX
        INNER JOIN (
            SELECT id, ticket_id, other_data, time_stamp FROM `ticket`
        ) CUR ON MAX.max_id = CUR.cur_id AND MAX.ticket_id = CUR.ticket_id
    ) T ON P.id=T.id
    WHERE
            P.since IS NOT NULL;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a relatively small database that I would like to generate a diagram
I have a relatively simple object with a method that makes an HTTP request.
I have a relatively simple question. Will using PHP guarantee that a form is
I have this relatively large numerical application code that may run for a few
I have a relatively small app that Im building using vb.net 2.0, and nant.
I have a somewhat complicated assortment of tables for which I need to do
I have a relatively complicated excel sheet in which columns start at A and
I have a relatively complicated app. I want to display a view over the
I have a relatively complicated generic type (say Map<Long,Map<Integer,String>> ) which I use internally
I have a project that I thought was going to be relatively easy, but

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.