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.