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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T11:09:23+00:00 2026-05-29T11:09:23+00:00

I have the following database structure: Sites table id | name | other_fields Backups

  • 0

I have the following database structure:

Sites table

id  |  name  |  other_fields

Backups table

id  |  site_id  | initiated_on(unix timestamp) | size(float) | status

So Backups table have a Many to One relationship with Sites table connected via site_id


And I would like to output the data in the following format

name | Latest initiated_on | status of the latest initiated_on row

And I have the following SQL query

SELECT *, `sites`.`id` as sid, SUM(`backups`.`size`) AS size
FROM (`sites`)
LEFT JOIN `backups` ON `sites`.`id` = `backups`.`site_id`
WHERE `sites`.`id` =  '1'
GROUP BY `sites`.`id`
ORDER BY `backups`.`initiated_on` desc

The thing is, with the above query I can achieve what I am looking for, but the only problem is I don’t get the latest initiated_on values.

So if I had 3 rows in backups with site_id=1, the query does not pick out the row with the highest value in initiated_on. It just picks out any row.

Please help, and

thanks in advance.

  • 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-29T11:09:24+00:00Added an answer on May 29, 2026 at 11:09 am

    You should try:

    SELECT sites.name, FROM_UNIXTIME(b.latest) as latest, b.size, b.status
    FROM sites
    LEFT JOIN
      ( SELECT bg.site_id, bg.latest, bg.sizesum AS size, bu.status
        FROM
          ( SELECT site_id, MAX(initiated_on) as latest, SUM(size) as sizesum
            FROM backups
            GROUP BY site_id ) bg
        JOIN backups bu
        ON bu.initiated_on = bg.latest AND bu.site_id = bg.site_id
      ) b
    ON sites.id = b.site_id
    
    1. In the GROUP BY subquery – bg here, the only columns you can use for SELECT are columns that are either aggregated by a function or listed in the GROUP BY part.

      http://dev.mysql.com/doc/refman/5.5/en/group-by-hidden-columns.html

    2. Once you have all the aggregate values you need to join the result again to backups to find other values for the row with latest timestamp – b.

    3. Finally join the result to the sites table to get names – or left join if you want to list all sites, even without a backup.

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

Sidebar

Related Questions

I have the following database structure: Event table Id - Guid (PK) Name -
I have no control over database schema and have the following (simplified) table structure:
I have the following database structure: CREATE TABLE LookupTable ( PK UNIQUEIDENTIFIER PRIMARY KEY,
I have the following database structure: Table 1 Table 2 Table 3 tid_1 ----(many-to-one)----
I have the following database structure (simplified) Store StoreId RateId Products ProductId Name Rates
I have the following database structure: CREATE TABLE `author` ( `id` int(10) unsigned NOT
I have two database tables with the following structure: actions: action_id int(11) primary key
I have the following database table created thus: CREATE TABLE AUCTIONS ( ARTICLE_NO VARCHAR(20),
Let's say I have the following database table: record_id | record_date | record_value -----------+-------------+--------------
I have the following table relationship in my database: Parent / \ Child1 Child2

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.