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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T20:13:53+00:00 2026-05-11T20:13:53+00:00

I have two tables: article(‘id’, ‘ticket_id’, ‘incoming_time’, ‘to’, ‘from’, ‘message’) ticket(‘id’, ‘queue_id’) where tickets

  • 0

I have two tables:

article('id', 'ticket_id', 'incoming_time', 'to', 'from', 'message')
ticket('id', 'queue_id')

where tickets represent a thread of emails between support staff and customers, and articles are the individual messages that compose a thread.

I’m looking to find the article with the highest incoming time (expressed as a unix timestamp) for each ticket_id, and this is the query I’m currently using:

SELECT article.* , MAX(article.incoming_time) as maxtime
FROM ticket, article
WHERE ticket.id = article.ticket_id
AND ticket.queue_id = 1
GROUP BY article.ticket_id

For example,

:article:
id --- ticket_id --- incoming_time --- to ------- from ------- message --------
11     1             1234567           help@      client@      I need help...   
12     1             1235433           client@    help@        How can we help?
13     1             1240321           help@      client@      Want food!    
...

:ticket:
id --- queue_id
1      1
...

But the result looks to be the row with the smallest article id instead of what I’m looking for which is the article with the highest incoming time.

Any advice would be greatly appreciated!

  • 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-05-11T20:13:54+00:00Added an answer on May 11, 2026 at 8:13 pm

    This is a classic hurdle that most MySQL programmers bump into.

    • You have a column ticket_id that is the argument to GROUP BY. Distinct values in this column define the groups.
    • You have a column incoming_time that is the argument to MAX(). The greatest value in this column over the rows in each group is returned as the value of MAX().
    • You have all other columns of table article. The values returned for these columns are arbitrary, not from the same row where the MAX() value occurs.

    The database cannot infer that you want values from the same row where the max value occurs.

    Think about the following cases:

    • There are multiple rows where the same max value occurs. Which row should be used to show the columns of article.*?

    • You write a query that returns both the MIN() and the MAX(). This is legal, but which row should article.* show?

      SELECT article.* , MIN(article.incoming_time), MAX(article.incoming_time)
      FROM ticket, article
      WHERE ticket.id = article.ticket_id
      AND ticket.queue_id = 1
      GROUP BY article.ticket_id
      
    • You use an aggregate function such as AVG() or SUM(), where no row has that value. How is the database to guess which row to display?

      SELECT article.* , AVG(article.incoming_time)
      FROM ticket, article
      WHERE ticket.id = article.ticket_id
      AND ticket.queue_id = 1
      GROUP BY article.ticket_id
      

    In most brands of database — as well as the SQL standard itself — you aren’t allowed to write a query like this, because of the ambiguity. You can’t include any column in the select-list that isn’t inside an aggregate function or named in the GROUP BY clause.

    MySQL is more permissive. It lets you do this, and leaves it up to you to write queries without ambiguity. If you do have ambiguity, it selects values from the row that is physically first in the group (but this is up to the storage engine).

    For what it’s worth, SQLite also has this behavior, but it chooses the last row in the group to resolve the ambiguity. Go figure. If the SQL standard doesn’t say what to do, it’s up to the vendor implementation.

    Here’s a query that can solve your problem for you:

    SELECT a1.* , a1.incoming_time AS maxtime
    FROM ticket t JOIN article a1 ON (t.id = a1.ticket_id)
    LEFT OUTER JOIN article a2 ON (t.id = a2.ticket_id 
      AND a1.incoming_time < a2.incoming_time)
    WHERE t.queue_id = 1
      AND a2.ticket_id IS NULL;
    

    In other words, look for a row (a1) for which there is no other row (a2) with the same ticket_id and a greater incoming_time. If no greater incoming_time is found, the LEFT OUTER JOIN returns NULL instead of a match.

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

Sidebar

Related Questions

I have two tables, news and news_views. Every time an article is viewed, the
I have two tables -- an article table and a vote table. Users can
I have two tables from two different databases. For example: Table: Articles1 - Database:
I have two tables Say Table-A and Table-B Table-A id | Article Name |date
I have two tables. One is article and the other structure. And the articles
I have two tables, like this: #Articles: ID | Title 1 Article title 2
suppose i have two tables. articles and comments. when i am selecting columns from
I have two tables that make up a full text index of article content
So I have two tables, article and comments (which has one-to-many relationship (1 article
I have two mysql tables, one listing article names, the other listing the authors

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.