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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T22:05:48+00:00 2026-06-05T22:05:48+00:00

My goal is to select articles where the primary_category_id ( articles table) or any

  • 0

My goal is to select articles where the primary_category_id (articles table) or any of the
secondary categories (articles_secondary_categories join table) are a given value. In this example query, category 1. I tried using other types of joins but the caveat here is that an article might not have any secondary categories.

SELECT DISTINCT articles.* 
FROM articles
LEFT JOIN articles_secondary_categories AS categories 
    ON categories.article_id = articles.id
WHERE 
(
    primary_category_id = 1
    OR
    categories.category_id = 1
)
    AND articles.state = "published"
    AND edition_id = 1

ORDER BY publish_at DESC
LIMIT 10;

Any help optimizing this or alternatives are welcome. In a DB with 4k articles and 7k articles_secondary_categories (not categories) it takes 5 seconds to run this query.

  • 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-05T22:05:50+00:00Added an answer on June 5, 2026 at 10:05 pm

    You can reverse the query on the secondary categories:

    (SELECT articles.* 
    FROM articles
    WHERE primary_category_id = 1)
    UNION DISTINCT
    (SELECT articles.*
    FROM articles_secondary_categories AS categories 
    JOIN articles ON (categories.article_id = articles.id)
    WHERE categories.category_id = 1
    GROUP BY articles_id)
    ORDER BY publish_at DESC
    LIMIT 10;
    

    It should give you a decent speed boost – just make sure you index categories.articles_id

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

Sidebar

Related Questions

so my main goal with this query is to select all departments and employees
My goal is to select value from EmbedImgDimension column where in lots of duplicated
The goal of this query is to get a total of unique records (most
Example: SELECT s.title, t.description, u1.value * s.cache_value as Cache, u2.value * s.drive_value as Drive,
I have a query like this: SELECT t1.id, (SELECT COUNT(t2.id) FROM t2 WHERE t2.id
my goal is to select items from a table and append those items into
I would like to perform a SELECT query with MySQL. My goal is to
This is what I am working with: http://jsfiddle.net/VkYnQ/ The goal is to select as
Goal: To display a category select-box and its corresponding subcategory select-box. MySQL table is
Main goal of this question is to create a table with easy-to-use, wide-spread (more

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.