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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T19:57:13+00:00 2026-05-16T19:57:13+00:00

I’d like to make a query where I can retrieve an interval of 10

  • 0

I’d like to make a query where I can retrieve an interval of 10 rows with 5 tags per each one.

I have houses and tags table, I know how to do it only for simple queries with a SELECT using LIMIT but how can I do it in this case?

table houses
id    house            country
1     Grunt Mansion    us
2     Hororo Suneku    jp
3     Casa Cattani     it
4     Sweet Home       uk
5     Heinzvaiter      de
6     F56X-5           us
7     Swan Jong        cn
8     Drupnowevo       ru
9     Bambagiador      br
10    El Stanco        es

table houses_tags
id    id_house    id_tag
1     1           1
2     1           2
3     1           3
4     1           4
5     2           1
6     2           2
7     2           3
8     2           4

table tags
id    tag
1     minimal
2     baroque
3     cosy
4     simple

If a make a query like this to get the first 10 houses:

SELECT *
FROM houses
LEFT JOIN (
    SELECT *
    FROM tags
    INNER JOIN houses_tags
    ON id_house = houses.id
    LIMIT 5
) house_tag
LIMIT 0, 10

I get something like this:

query result
row    house            country    tag      id_house     id_tag
1      Grunt Mansion    us         minimal  1            1
2      Grunt Mansion    us         baroque  1            2
3      Grunt Mansion    us         cosy     1            3
4      Grunt Mansion    us         simple   1            4
5      Hororo Suneku    jp         minimal  2            1
6      Hororo Suneku    jp         baroque  2            2
7      Hororo Suneku    jp         cosy     2            3
8      Hororo Suneku    jp         simple   2            4
9      Casa Cattani     it         NULL     NULL         NULL
10     Sweet Home       uk         NULL     NULL         NULL

My problem is I get only the first 10 rows cutting out the last houses from the query because the tags of the first ones occupy all the rows

Can I write a query where I can retrieve the first 10 houses and 5 tag per each one?

query result
row    house            country    tag      id_house     id_tag
1      Grunt Mansion    us         minimal  1            1
2      Grunt Mansion    us         baroque  1            2
3      Grunt Mansion    us         cosy     1            3
4      Grunt Mansion    us         simple   1            4
5      Hororo Suneku    jp         minimal  2            1
6      Hororo Suneku    jp         baroque  2            2
7      Hororo Suneku    jp         cosy     2            3
8      Hororo Suneku    jp         simple   2            4
9      Casa Cattani     it         NULL     NULL         NULL
10     Sweet Home       uk         NULL     NULL         NULL
11     Heinzvaiter      de         NULL     NULL         NULL
12     F56X-5           us         NULL     NULL         NULL
13     Swan Jong        cn         NULL     NULL         NULL
14     Drupnowevo       ru         NULL     NULL         NULL
15     Bambagiador      br         NULL     NULL         NULL
16     El Stanco        es         NULL     NULL         NULL

At the end I should need a result I display like this example:

Mansions          Tags
Grunt Mansion     minimal, baroque, cosy, simple
Hororo Suneku     minimal, baroque, cosy, simple
Casa Cattani      -
Sweet Home        -
Heinzvaiter       -
F56X-5            -
Swan Jong         -
Drupnowevo        -
Bambagiador       -
El Stanco         -

pages 1 | 2 | 3

can I do it?

  • 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-16T19:57:14+00:00Added an answer on May 16, 2026 at 7:57 pm

    MySQL doesn’t have analytic functions, which is what you’d use for this before LIMIT:

        SELECT x.id,
               x.house,
               x.country,
               x.id_tag
          FROM (SELECT h.id,
                       h.house,
                       h.country,
                       ht.id_tag,
                       CASE 
                         WHEN @id = h.id THEN @rownum := @rownum + 1
                         ELSE @rownum := 1
                       END AS rank,
                       @id := h.id,
                       @house_count := @house_count + 1 AS house_count
                  FROM HOUSE h
             LEFT JOIN HOUSES_TAGS ht ON ht.id_house = h.id
                  JOIN (SELECT @rownum := 0, @id := -1, @house_count := 0) r
              ORDER BY h.id, ht.id_tag) x
       WHERE x.house_count <= 10
         AND x.rank <= 4        
    ORDER BY x.id, x.id_tag
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables with like below codes: Table: Accounts id | username |
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a jquery bug and I've been looking for hours now, I can't
Basically, what I'm trying to create is a page of div tags, each has
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
For some reason, after submitting a string like this Jack’s Spindle from a text
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
I've got a string that has curly quotes in it. I'd like to replace

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.