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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:11:31+00:00 2026-06-17T15:11:31+00:00

I have this query with multiple tables and I got the following result. Then

  • 0

I have this query with multiple tables and I got the following result. Then I want to group by t.id. I know I can simply use group by t.id but how can I select the latest row of t.id which is url tw5.jpg and created_time 10000004

SELECT p.url,t.name,t.num_photo,t.id
FROM photos AS p
LEFT JOIN tag_maps AS tm ON p.id = tm.photo_id
LEFT JOIN tags AS t ON t.id = tm.tag_id

url                       created_time    name      num_photo  id
assets/img/tags/tw1.jpg   1000001         my house  1           1
assets/img/tags/tw2.jpg   1000002         dog       1           2
assets/img/tags/tw3.jpg   1000003         taiwan    2           3
assets/img/tags/tw5.jpg   1000004         taiwan    2           3

This is what i get when using group by t.id

url                       created_time    name      num_photo  id
assets/img/tags/tw1.jpg   1000001         my house  1           1
assets/img/tags/tw2.jpg   1000002         dog       1           2
assets/img/tags/tw3.jpg   1000003         taiwan    2           3

And this is what i want

url                       created_time    name      num_photo  id
assets/img/tags/tw1.jpg   1000001         my house  1           1
assets/img/tags/tw2.jpg   1000002         dog       1           2
assets/img/tags/tw5.jpg   1000004         taiwan    2           3

EDITED

Photos table

id    url                      created_time
1     assets/img/tags/tw1.jpg  1000001
2     assets/img/tags/tw2.jpg  1000002
3     assets/img/tags/tw3.jpg  1000003
4     assets/img/tags/tw5.jpg  1000004

Tags table

id name     num_photo
1  my house 1
2  dog      1
3  taiwan   2

Tag_maps

id   tag_id   photo_id
1    1        1
2    2        2
3    3        3
4    3        4
  • 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-17T15:11:33+00:00Added an answer on June 17, 2026 at 3:11 pm

    Edit #2: Without seeing sample data from each table or the table structures, I am guessing that the following will work:

    SELECT p.url,
      created_time,
      t.name,
      t.num_photo,
      t.id
    FROM photos p
    LEFT JOIN tag_maps AS tm 
        ON p.id = tm.photo_id
    LEFT JOIN tags AS t 
        ON t.id = tm.tag_id
    INNER JOIN
    (
      select max(created_time) MaxDate, t.id
      FROM photos p
      LEFT JOIN tag_maps AS tm 
          ON p.id = tm.photo_id
      LEFT JOIN tags AS t 
          ON t.id = tm.tag_id
      group by t.id
    ) d
      on p.created_time = d.MaxDate
      and t.id = d.id;
    

    See SQL Fiddle with Demo

    Or another way to write this is with a subquery that returns the max(photo_id) by tag_Id from the tag_maps table and use that result in the join:

    SELECT p.url,
      created_time,
      t.name,
      t.num_photo,
      t.id
    FROM photos p
    INNER JOIN
    (
      select max(photo_id) photo_id, tag_id
      from tag_maps
      group by tag_id
    ) AS tm 
        ON p.id = tm.photo_id
    LEFT JOIN tags AS t 
        ON t.id = tm.tag_id
    

    See SQL Fiddle with Demo

    Result is:

    |                     URL | CREATED_TIME |     NAME | NUM_PHOTO | ID |
    ----------------------------------------------------------------------
    | assets/img/tags/tw1.jpg |      1000001 | my house |         1 |  1 |
    | assets/img/tags/tw2.jpg |      1000002 |      dog |         1 |  2 |
    | assets/img/tags/tw5.jpg |      1000004 |   taiwan |         2 |  3 |
    


    Edit #1 since your url is different and you want the max(id), then you should be able to use:

    SELECT p.url,
      t.name,
      t.num_photo,
      Max(t.id) id
    FROM photos p
    LEFT JOIN tag_maps AS tm 
        ON p.id = tm.photo_id
    LEFT JOIN tags AS t 
        ON t.id = tm.tag_id
    group by t.name, t.num_photo  
    

    OP: You can use a subquery:

    SELECT p.url,
        t.name,
        t.num_photo 
    FROM
    (
        select MAX(created_time) created_time, url, id
        from photos
        group by url, id
    )  AS p
    LEFT JOIN tag_maps AS tm 
        ON p.id = tm.photo_id
    LEFT JOIN tags AS t 
        ON t.id = tm.tag_id
    

    If the id value in the photos table is unique for each row, then you might need to use the following:

    SELECT p1.url,
        t.name,
        t.num_photo 
    FROM photos p1
    inner join
    (
        select MAX(created_time) created_time, url
        from photos
        group by url
    )  AS p2
        on p1.url = p2.url
        and p1.created_time = p2.created_time
    LEFT JOIN tag_maps AS tm 
        ON p1.id = tm.photo_id
    LEFT JOIN tags AS t 
        ON t.id = tm.tag_id
    

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

Sidebar

Related Questions

All... I need a way to query identical tables across multiple databases. I have
I have a query that is getting counts from multiple tables by using a
I'm trying to query multiple tables at once. Say I have a table named
I have joined multiple tables with INNER JOIN and got the output which resembles
I've got a SQL query running a FULL JOIN on multiple database tables. The
I got a minor problem. To start, I have the following tables: 'groups', 'pages'
Have this query: SELECT HOUR( DATE ) AS hr, COUNT( * ) AS cnt
I have this query... SELECT Distinct([TargetAttributeID]) FROM (SELECT distinct att1.intAttributeID as [TargetAttributeID] FROM AST_tblAttributes
I have this query which on executing in my sql command line client executes
i have this query: SELECT `completed`.`ID` AS `ID`,`completed`.`level` AS `level`,`completed`.`completed_in` AS `completed_in`, COUNT(1) AS

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.