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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:51:02+00:00 2026-05-25T21:51:02+00:00

I am working on a property website and have record sets for property and

  • 0

I am working on a property website and have record sets for property and for unit, unit has a one-to-many relationship with property. What I’m trying to figure out is how to best create a search function which will output results based on criteria from both. So if I search for a property with the location Manchester and a unit with a freehold tenure I’d like to eliminate all properties which don’t have a unit with the tenure of freehold.

A potential solution I’ve considered is to create a record set for properties which match the property criteria and then create a unit record set for units which match the unit criteria and then finally loop through the property record set in server-side code and eliminate any properties which aren’t related to any of the units in the unit record set. Really not sure if this is the best way to do things though so would be keen to hear any suggestions?

Thanks

EDIT (Added table structure and MySQL):

--
-- Table structure for table `property`
--

CREATE TABLE IF NOT EXISTS `property` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` text NOT NULL,
  `street` text NOT NULL,
  `town` text NOT NULL,
  `postcode` text NOT NULL,
  `description` longtext NOT NULL,
  `team_member` varchar(255) NOT NULL DEFAULT '',
  `pdf` text NOT NULL,
  `default_image_id` int(11) DEFAULT NULL,
  `virtual_tour_link` text NOT NULL,
  `date` date NOT NULL DEFAULT '0000-00-00',
  `archive` int(11) NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COMMENT='' AUTO_INCREMENT=13 ;

--
-- Table structure for table `unit`
--

CREATE TABLE IF NOT EXISTS `unit` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` text NOT NULL,
  `description` text NOT NULL,
  `size_sq_ft` int(11) DEFAULT NULL,
  `size_acres` float DEFAULT NULL,
  `price` float DEFAULT NULL,
  `rental_price` float DEFAULT NULL,
  `on_application` tinyint(1) DEFAULT NULL,
  UNIQUE KEY `id` (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COMMENT='Stores data for property units' AUTO_INCREMENT=5;

--
-- Table structure for table `property_to_unit`
--

CREATE TABLE IF NOT EXISTS `property_to_unit` (
  `property_id` int(11) NOT NULL,
  `unit_id` int(11) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;


--
-- MySQL which produces list of properties
--

SELECT
    P.id AS id,
    P.name AS name,
    P.street AS street,
    P.town AS town,  
    P.postcode AS postcode,  
    P.description AS description,
    P.team_member AS team_member,
    P.pdf AS pdf,
    P.virtual_tour_link AS virtual_tour_link,
    P.date AS date,
    P.archive AS archive,
    PI.name as image,
    P2.image_ids as image_ids,
    L2.location_ids as location_ids,
    U2.unit_ids as unit_ids

FROM property P

-- Get default image and join using property id

LEFT JOIN property_image PI ON PI.id = P.default_image_id

-- Create a list of image_ids from property_image and
-- property_to_property_image tables then join using property_id

LEFT JOIN (

    SELECT
        property_id,
        GROUP_CONCAT(CAST(id AS CHAR)) as image_ids
    FROM property_to_property_image PTPI
    LEFT JOIN property_image PI ON PI.id = PTPI.property_image_id
    GROUP BY property_id

) P2 ON P2.property_id = P.id

-- Create a list of locations from property_location table
-- and join using property_id

LEFT JOIN (
    SELECT
        property_id,
        property_location_id,
        GROUP_CONCAT(CAST(property_location.id AS CHAR)) AS location_ids
    FROM property_to_property_location
    INNER JOIN property_location ON property_location.id = property_to_property_location.property_location_id
    GROUP BY property_id
) L2 ON L2.property_id = P.id

-- Create a list of units from unit table
-- and join using property_id

LEFT JOIN (
    SELECT
        property_id,
        unit_id,
        GROUP_CONCAT(CAST(unit_id AS CHAR)) AS unit_ids
    FROM property_to_unit
    INNER JOIN unit ON unit.id = property_to_unit.unit_id
    GROUP BY property_id
) U2 ON U2.property_id = P.id

--
-- MySQL which produces list of units
--

SELECT 
id,
name,
description,
size_sq_ft,
size_acres,
price,
rental_price,
on_application,
tenure_ids,
tenure_names,
type_ids,
type_names
FROM unit AS U

-- join tenure ids and names

LEFT JOIN (

    SELECT
        unit_id,
        GROUP_CONCAT( CAST(UT.id AS CHAR) ) AS tenure_ids,
        GROUP_CONCAT(UT.name) AS tenure_names
    FROM unit_to_unit_tenure UTUT
    INNER JOIN unit_tenure UT ON UT.id = UTUT.unit_tenure_id
    GROUP BY unit_id

) UT ON UT.unit_id = U.id

-- join type ids and names

LEFT JOIN (

    SELECT
        unit_id,
        GROUP_CONCAT( CAST(UTYPE.id AS CHAR) ) AS type_ids,
        GROUP_CONCAT(UTYPE.name) AS type_names
    FROM unit_to_unit_type UTUT
    INNER JOIN unit_type UTYPE ON UTYPE.id = UTUT.unit_type_id
    GROUP BY unit_id

) UTYPE ON UTYPE.unit_id = U.id

WHERE 0=0

I’m currently using a dynamically created WHERE statement appended to each MySQL query to filter the property and unit results.

  • 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-25T21:51:03+00:00Added an answer on May 25, 2026 at 9:51 pm

    You’re making it a bit more complicated than it is. If I understand correctly, you can easily do this in a single query. This would search properties that have units with a particlar unit tenure id:

    select * 
    from property p 
    where p.id in (
        select pu.property_id
        from property_to_unit pu
            inner join unit u ON pu.unit_id = u.id
            inner join unit_to_unit_tenure uut ON u.id = uut.unit_id
        where uut.id = <cfqueryparam value="#uutid#">
    )
    

    Using two queries and then looping through to cross-check sounds like it could be dog slow.

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

Sidebar

Related Questions

I am working on a website which have pages arranged in one table with
I am working in one real-estate website and I have large database around 250
I am working on a custom control and have created a property in property
I'm trying to bind element's Height value to Checkbox.IsChecked property. Why that's not working?
Im working on something incredibly unique..... a property listings website. ;) It displays a
I'm working with .Net using VB and have just finished a website project and
I am working on a Joomla1.5 website. I have two navigation menus in the
I'm working on a website where disabled access is one of the primary requirements.
I have an ASP.NET Razor website that is working properly when I test locally
So I am working on localization for a website, and I have ran into

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.