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

  • Home
  • SEARCH
  • 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 5974927
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:00:40+00:00 2026-05-22T21:00:40+00:00

In my PHP application, I have a mysql table of articles which has the

  • 0

In my PHP application, I have a mysql table of articles which has the following columns:

article_id    articletext    category_id    score

Each article has a score which is calculated based on how popular it is, and belongs to a specific category (there are around 10 categories available)

My question is: how can I perform a query that returns the highest scored articles while alternating them by categories so that if possible, no same-category articles are returned consecutively.
For example if the highest scored article has score: 100 the returning set would be something like:

article_id    articletext    category_id    score
-----------------------------------------------------
142           <.....>        5              100
153           <.....>        3              97
119           <.....>        5              99
169           <.....>        2              93
121           <.....>        7              89
197           <.....>        2              92
.
.
.

The first (naive) solution that comes in mind is performing 10 selects (1 for each category), ordering them by score descending, and then, in PHP level, alternating between each returned dataset, picking one result at a time and combining them together in a new array.

Is there any more efficient way to achieve this? If possible at the MySQL level

  • 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-22T21:00:41+00:00Added an answer on May 22, 2026 at 9:00 pm

    Go get the top 20. If they don’t satisfy the requirements, do an additional query to get the missing pieces. You should be able to come up with some balance between number of queries and number of rows each returns.

    I you got the top 100 it might satisfy the requirements 90% of the time and would be cheaper and faster than 10 separate queries.

    If it was SQL Server I could help more…

    Actually, I have another idea. Run a process every 5 minutes that calculates the list and caches it in a table. Make DML against related tables invalidate the cache so it is not used until repopulated (perhaps an article was deleted). If the cache is invalid, you would fall back to calculating it on the fly… And could use that to repopulate the cache anyway.

    It might be possible to strategically update the cached list rather than recalculate it. But that could be a real challenge.

    This should help both with query speed and reducing load on your database. It shouldn’t matter much if your article list is 5 minutes out of date. Heck, even 1 minute might work.

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

Sidebar

Related Questions

I have a PHP MySQL application which has a modular design. There are about
I have a PHP web application which uses a MySQL database for object tagging,
I am in the process of building a PHP/MySql database application which could have
I am working on an inventory php/mysql application. I have a main table (inv_main)
I'm writng a small application in PHP + MySQL and have come to the
Hey everyone, I have the following 'users' table in MySQL: CREATE TABLE `users` (
I have a mysql question. I have an application which I am migrating to
We have a PHP/MySQL application and I want to setup a logging mechanism to
I have some PHP script for export MYSQL table into Excel format, but I
I have, at its heart, a php/mysql application. Every ~15 minutes new data is

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.