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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T08:17:59+00:00 2026-06-08T08:17:59+00:00

I have two tables in my database: categories id category message messages id title

  • 0

I have two tables in my database:

categories

id
category
message

messages

id
title
message

I am trying to retrieve two messages with their categories. Every message can have multiple categories. I’ve tried with the following query:

SELECT categories.category, messages.id, messages.title, messages.message
FROM categories
RIGHT JOIN messages
ON messages.id = category.message
ORDER BY messages.id DESC
LIMIT 2
OFFSET 0

The ouput for this is something like:

category   id   title        message
test-cat   1    Test title   This is the message body
category2  1    Test title   This is the message body

However this query results in only two rows (because the message which is retrieved has multiple categories). How can I limit on the number of messages rather than the number of categories? So that the result is like:

category   id   title        message
test-cat   1    Test title   This is the message body
category2  1    Test title   This is the message body
test-cat   2    Another msg  This is content
test-cat2  2    Another msg  This is content
something  2    Another msg  This is content
  • 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-08T08:18:00+00:00Added an answer on June 8, 2026 at 8:18 am

    Put the limit in a subquery:

    SELECT categories.category, m.id, m.title, m.message
    FROM categories RIGHT JOIN
         (select *
          from messages
          ORDER BY messages.id DESC
          limit 2
         ) m
         ON m.id = categories.message
    ORDER BY m.id DESC
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's assume I have a database with two tables: categories and articles . Every
I have two database tables, grouptypes and groups. A grouptype can have multiple groups,
I have two tables in my database - Category and Food. Food has idCategory.
I have two tables: Category (Id, Name, ...) and Question (Id, CategoryId, Title, ...)
Let's say I have two tables in my database. TABLE:Categories ID|CategoryName 01|CategoryA 02|CategoryB 03|CategoryC
I have a two same tables.Database mysql. How can I compare two tables? Table1,Table2
I have two database tables, Categories and SuperCategories, for an inventory control system I'm
I have two models : Category and Picture which refers to two tables, Categories
I have two tables of data categories and category relations. This is a setup
I have a database with two tables: Users and Categories . Users has these

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.