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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T08:20:26+00:00 2026-05-11T08:20:26+00:00

I have this query: SELECT p.id, r.status, r.title FROM page AS p INNER JOIN

  • 0

I have this query:

SELECT p.id, r.status, r.title FROM page AS p     INNER JOIN page_revision as r ON r.pageId = p.id AND (         r.id = (SELECT MAX(r2.id) from page_revision as r2 WHERE r2.pageId = r.pageId AND r2.status = 'active')         OR r.id = (SELECT MAX(r2.id) from page_revision as r2 WHERE r2.pageId = r.pageId)     ) 

Which returns each page and the latest active revision for each, unless no active revision is available, in which case it simply returns the latest revision.

Is there any way this can be optimised to improve performance or just general readability? I’m not having any issues right now, but my worry is that when this gets into a production environment (where there could be a lot of pages) it’s going to perform badly.

Also, are there any obvious problems I should be aware of? The use of sub-queries always bugs me, but to the best of my knowledge this cant be done without them.

Note:
The reason the conditions are in the JOIN rather than a WHERE clause is that in other queries (where this same logic is used) I’m LEFT JOINing from the ‘site’ table to the ‘page’ table, and If no pages exist I still want the site returned.

Jack

Edit: I’m using MySQL

  • 1 1 Answer
  • 2 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. 2026-05-11T08:20:27+00:00Added an answer on May 11, 2026 at 8:20 am

    If ‘active’ is the first in alphabetical order you migt be able to reduce subqueries to:

    SELECT p.id, r.status, r.title FROM page AS p     INNER JOIN page_revision as r ON r.pageId = p.id AND          r.id = (SELECT r2.id                  FROM page_revision as r2                  WHERE r2.pageId = r.pageId                  ORDER BY r2.status, r2.id DESC                 LIMIT 1) 

    Otherwise you can replace ORDER BY line with

    ORDER BY CASE r2.status WHEN 'active' THEN 0 ELSE 1 END, r2.id DESC 

    These all come from my assumptions on SQL Server, your mileage with MySQL may vary.

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

Sidebar

Related Questions

I have this sql query: SELECT S.SEARCH, S.STATUS, C.TITLE AS CategoryName, E.SEARCH_ENGINES AS Engine,
I have this query. SELECT notes.id,enter.name as 'enter_name',step.title as 'flow status',notes.user_name as user_created,notes.created,notes.rel_client_id,td_doc_nr.value_string as
I have this query: SELECT * From checkfinale where AssociateID = 51 AND `CompletedDate`
I have this query: select BUnit, value from myTable where BUnit in (555,556,557,558) and
I have this query in my sql if (select count(*) from sys.columns where object_id
I have this query: (SELECT e.IdEvent,e.EventName,e.EventSubtitle,e.EventDescription,l.LocationName,e.EventVenue,EventStartDate,e.EventEndDate,e.EventHost,c.CategoryName,l.LocationCity,l.LocationState,e.isTBA, (SELECT s.status FROM jos_rsevents_subscriptions s WHERE s.IdUser =
I have this query: select qa_returns_items.item_code, (CASE status_code WHEN 11 THEN (qa_returns_items.item_quantity - qa_returns_residues.item_quantity)
Have this query: SELECT HOUR( DATE ) AS hr, COUNT( * ) AS cnt
i have this query: SELECT `completed`.`ID` AS `ID`,`completed`.`level` AS `level`,`completed`.`completed_in` AS `completed_in`, COUNT(1) AS
I have this query SELECT zip, ( 3959 * acos( cos( radians(34.12520) ) *

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.