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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T02:16:08+00:00 2026-05-17T02:16:08+00:00

I’m selecting properties and joining them to mapping tables where they get mapped to

  • 0

I’m selecting properties and joining them to mapping tables where they get mapped to filters such as location, destination, and property type.

My goal is to grab all the properties and then LEFT JOIN them to the tables, and then basically get data that shows all the locations, destinations a property is attached to and the property type itself.

Here’s my query:

SELECT p.slug                                        AS property_slug, 
       p.name                                        AS property_name, 
       p.founder                                     AS founder, 
       IF (p.display_city != '', display_city, city) AS city, 
       d.name                                        AS state,
       type
       GROUP_CONCAT( CONVERT(subcategories_id, CHAR(8)) )  AS foo,
       GROUP_CONCAT( CONVERT(categories_id, CHAR(8)) ) AS bah
     FROM properties AS p 
LEFT JOIN destinations AS d ON d.id = p.state 
LEFT JOIN regions AS r ON d.region_id = r.id 
LEFT JOIN properties_subcategories AS sc ON p.id = sc.properties_id 
LEFT JOIN categories_subcategories AS c  ON c.subcategory_id = sc.subcategories_id 
    WHERE 1 = 1 
      AND p.is_active = 1       
GROUP  BY p.id 

Before I do the GROUP BY and GROUP_CONCAT my data looks like this:

id  name                  type     category_id    subcategory_id    state
--------------------------------------------------------------------------
1   The Hilton Hotel      1        1              2                 7
1   The Hilton Hotel      1        1              3                 7
1   The BlaBla Resort     2        2              5                 7

After the GROUP BY and GROUP_CONCAT it becomes…

id  name                  type     category_id    subcategory_id    state
--------------------------------------------------------------------------
1   The Hilton Hotel      1        1, 1           2, 3              7
1   The BlaBla Resort     2        1              3                 7

Is this the preferred way of grabbing all the possible mappings for the property in one go, with GROUP_CONCAT into a CSV like this?

Using this data, I can render something like…

<div class="property" categories="1" subcategories="2,3">
   <h2>{property_name}</h2>
   <span>{property_location}</span>
</div>

Then use Javascript to show/hide based on if the user clicks on an anchor which has say, a subcategory="2" attribute it would hide each .property that doesn’t have 2 inside of its subcategories attribute value.

  • 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-17T02:16:09+00:00Added an answer on May 17, 2026 at 2:16 am

    I believe you want something like this:

    CREATE TABLE property (id INT NOT NULL PRIMARY KEY, name TEXT);
    
    INSERT
    INTO    property
    VALUES
            (1, 'Hilton'),
            (2, 'Astoria');
    
    CREATE TABLE category (id INT NOT NULL PRIMARY KEY, property INT NOT NULL);
    
    INSERT
    INTO    category
    VALUES
            (1, 1),
            (2, 1),
            (3, 2);
    
    CREATE TABLE subcategory (id INT NOT NULL PRIMARY KEY, category INT NOT NULL);
    
    INSERT
    INTO    subcategory
    VALUES
            (1, 1),
            (2, 1),
            (3, 2),
            (5, 3),
            (6, 3),
            (7, 3);
    
    
    SELECT  id, name,
            CONCAT(
            '{',
            (
            SELECT  GROUP_CONCAT(
                    '"', c.id, '": '
                    '[',
                    (
                    SELECT  GROUP_CONCAT(sc.id ORDER BY sc.id SEPARATOR ', ' )
                    FROM    subcategory sc
                    WHERE   sc.category = c.id
                    ),
                    ']' ORDER BY c.id SEPARATOR ', ')
            FROM    category c
            WHERE   c.property = p.id
            ), '}')
    FROM    property p;
    

    which would output this:

    1   Hilton     {"1": [1, 2], "2": [3]}
    2   Astoria    {"3": [5, 6, 7]}
    

    The last field is a properly formed JSON which maps category id’s to the arrays of subcategory id’s.

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

Sidebar

Related Questions

No related questions found

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.