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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T05:02:37+00:00 2026-05-30T05:02:37+00:00

The first question your probably asking is how many Group by / Order by

  • 0

The first question your probably asking is how many Group by / Order by questions are there on SO? A lot. None of which are fitting my specific circumstance. Here is my following SQL query.

The goal is to get the Latest media for each team in $teamArr (a set of unique ids).

The problem I face is that it will not get the latest media for each, but its based on the order of the group by which is the time of media independent of the team.

Here is the MySQL command (With PHP’s sprintf() command for ease of use with the team Ids).

sprintf("SELECT M.*, T.name, T.has_image
    FROM media AS M
        JOIN teams AS T
          ON T.team_uid = M.team_uid
    WHERE M.team_uid IN (%s)
    GROUP BY T.team_uid
    ORDER BY M.time DESC", implode(", ", $teamArr));

Scenario:

team A has a media item of 3 hours ago, team B has 2 items 6 hours ago and 2 days ago, and Team C has a media item of 9 hours ago.

If the order of the select turns out to be Team A, C, B then the media items will be as follows…

  • TeamA’s 3 hours ago media item
  • TeamC’s 9 hours ago media item
  • TeamB’s 2 days ago media item

There will probably be some silly remarks saying

Just order by team name dummy! Such a simple problem. (this answer will get about +4 votes)

But that clearly does not help me at all. I cannot know what teams will have the optimal order (if any natural or unnatural ordering will even work with more than 3 cases). Is there such a way that I can do it in 1 SQL query. I hear that MySQL in a loop is a poor decision, hence the reason why i want this to work in 1 SQL call.

  • 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-30T05:02:38+00:00Added an answer on May 30, 2026 at 5:02 am

    This sort of query is usually tagged “greatest-n-per-group” for future reference.

    You want the maximum time per team and the corresponding columns, right?

    The typical way to solve this is to LEFT JOIN the table you want to get the max of to itself (in your case media):

    SELECT M.*, T.name, T.has_image
    FROM media AS M
    LEFT JOIN media AS M2              -- new: join media to itself
           ON M.team_uid = M2.team_uid -- within each team_uid (GROUP BY var)
          AND M.time < M2.time         -- and a sort on time (ORDER BY var)
    JOIN teams AS T              -- same as before.
     ON T.team_uid = M.team_uid
    WHERE M.team_uid IN (%s)     
      AND M2.time IS NULL        -- new: this selects the MAX M.time for each team.
    

    The changes:

    • LEFT JOIN media to itself within each team_uid (the GROUP BY variable), and such that M.time < M2.time (the variable we want the MAX of).
    • since it’s a LEFT JOIN, if there is a M.time for which there is no larger M2.time (for the same team), then M2.time will be NULL. This is precisely when M.time is the MAX time for that team.
    • add the M2.time IS NULL in the WHERE condition (to enforce the above)
    • remove the GROUP BY, ORDER BY (dealt with in the join condition).
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey, first of all thanks for helping me out with this (probably stupid) question
first question here. I'm developing a program in C# (.NET 3.5) that displays files
First question on here so please be nice :) I know very little about
First question on Stackoverflow (.Net 2.0): So I am trying to return an XML
First question here: it is a very short yet fundamental thing in Java that
First question from me; I'm currently fixing a graphing service that uses XSLFO to
First question is, given a url to an mp4 video, how can I save
First question here so hello everyone. The requirement I'm working on is a small
(First question related to iPhone development, so apologies for sounding off-track.) I'm creating a
My first question =). I'm writing a video game with a user interface written

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.