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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T12:03:14+00:00 2026-06-10T12:03:14+00:00

Okay, we have a database, with a not-so-good tables-columns layout, for a small news

  • 0

Okay, we have a database, with a not-so-good tables-columns layout, for a small news agency functioning online. Changing the layout is, currently, not possible; so, I’m focused on the queries.

Webpage requires a list of “guest” authors. Each list item should contain:

  • Author’s image (Authors)
  • Author’s name (Authors)
  • Title of the last article he/she wrote (News)
  • URI of that article (News)

In parentheses are the table names that info is stored in.

In “Authors” (40+ records), relevant columns are:

  • Image
  • Name
  • Type (we’ll look for the value “Guest” here)
  • ASCII (stores the “Name” value but without non-English characters and spaces)

The “News” (28k+ records) table is, basically, a pool where all the news and articles are pouring in. Columns of interest would be:

  • Id (primary key: the higher, the more recent)
  • Category
  • Title
  • URI

Now, “Category” has a whole bunch of values; however, if the particular record is an article, this column holds the “ASCII” of “Authors” (rather than its “Id”, sadly).

There was a PHP code, “while”ing queries for each author. I wanted to replace that when I saw it. So, I thought, “Hmm… How to fetch these in one go?” and came up with this:

-- Aliases are prefixed with 't' for tables and 'c' for columns.
SELECT
  tAu.Image, tAu.Name, tNw.Title, tNw.URI
FROM ( -- tAu & tNw

    SELECT * FROM ( -- tRc & Authors

        SELECT
          MAX(Id) cId, Category cCt -- Max Id for most recent
        FROM
          News
        GROUP BY
          cCt

    ) tRc -- table of categories with their most recent id's
    INNER JOIN
      Authors
    ON
      tRc.cCt = Authors.ASCII
    WHERE
      Authors.Type = 'Guest'

) tAu -- table of authors with their most recent id's
INNER JOIN
  News tNw
ON
  tAu.cId = tNw.Id -- merging authors with their latest article info

Currently, this query takes around 0.0364 seconds. May not be that bad; but, I am curious if this can be made any better (as this query SELECTs twice from News).

  • 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-10T12:03:15+00:00Added an answer on June 10, 2026 at 12:03 pm

    I don’t know if it is faster or not, but cleaned up your query a bit, which may help the optimizer:

    SELECT
      Authors.Image,
      Authors.Name,
      News.Title,
      News.URI
    
    FROM (
        SELECT
          MAX(Id) TopNewsId,
          Category
        FROM News
        GROUP BY Category
    ) TopNewsPerCats
    
    INNER JOIN News     ON News.Id       = TopNewsPerCats.TopNewsId
    
    INNER JOIN Authors  ON Authors.ASCII = TopNewsPerCats.Category
                       AND Authors.Type  = 'Guest'
    

    News table should have an index starting with ( Category, Id) to speed up the internal SELECT GROUP BY.

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

Sidebar

Related Questions

Okay, so I have a MySQL database set up. Most of the tables are
Okay, so let's say I have a mysql database table with two columns, one
Okay, I have a database, but no tables in it. I have some entity
Okay, so I have a database project for my database class. I have a
To anyone who can help, Thanks Okay, so I have a database holding a
Okay I have updated my code a little, but I am still not exactly
Okay, I want to have an app that takes phone numbers from an online
Okay, so I have a database that stores the name of an uploaded file
Okay. I have a database that has bunch of stored procedures and references another
I have a MySQL database table with this structure: table id INT NOT NULL

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.