tags:

views:

48

answers:

4

Here is the query that I am working on:

SELECT `unitid`, `name` FROM apartmentunits
WHERE aptid = (
    SELECT `aptid` FROM rentconditionsmap WHERE rentcondid = 1 AND condnum = 1
)

What I am having trouble figuring out is how to write this to add more rentcondition limiters to filter this list down more.

SELECT `aptid` FROM rentconditionsmap WHERE rentcondid = 1 AND condnum = 1

Data:

CREATE TABLE IF NOT EXISTS `rentconditionsmap` (
   `rcid` bigint(10) unsigned NOT NULL AUTO_INCREMENT, 
   `rentcondid` int(3) unsigned NOT NULL, 
   `condnum` tinyint(3) unsigned NOT NULL, 
   `aptid` bigint(10) unsigned DEFAULT NULL, 
   PRIMARY KEY (`rcid`), KEY `aptid` (`aptid`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=18 ;

INSERT INTO `rentconditionsmap` 
  (`rcid`, `rentcondid`, `condnum`, `aptid`) 
VALUES 
  (1, 1, 1, 1), 
  (2, 2, 1, 1), 
  (3, 3, 0, 1), 
  (4, 4, 1, 1), 
  (5, 8, 0, 1);

CREATE TABLE IF NOT EXISTS `apartmentunits` (
  `unitid` bigint(10) NOT NULL AUTO_INCREMENT, 
  `aptid` bigint(10) NOT NULL, 
  `name` varchar(6) NOT NULL, 
  `verified` tinyint(1) NOT NULL DEFAULT '0', 
  `rentcost` int(4) unsigned DEFAULT NULL, 
  `forrent` tinyint(1) NOT NULL DEFAULT '0', 
  `unittypekey` varchar(2) DEFAULT NULL, 
  `sqft` smallint(6) DEFAULT NULL, 
  PRIMARY KEY (`unitid`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=121 ;

INSERT INTO `apartmentunits` 
  (`unitid`, `aptid`, `name`, `verified`, `rentcost`, `forrent`, `unittypekey`, `sqft`) 
VALUES 
  (1, 1, '3', 1, 540, 0, '2B', NULL), 
  (2, 1, '5', 1, NULL, 0, '2B', NULL), 
  (3, 1, '7', 1, NULL, 0, '2B', NULL), 
  (53, 1, '1', 1, NULL, 0, '2B', NULL), 
  (54, 1, '2', 1, NULL, 0, '2B', NULL), 
  (55, 1, '4', 1, 570, 0, '2B', NULL), 
  (56, 1, '6', 1, NULL, 0, '2B', NULL), 
  (57, 1, '8', 1, NULL, 0, '2B', NULL), 
  (58, 1, '9', 1, NULL, 0, '2B', NULL), 
  (59, 1, '10', 1, NULL, 0, '2B', NULL), 
  (60, 1, '11', 1, NULL, 0, '2B', NULL);
A: 

Use a JOIN (below is TSQL sintax for a join, or you can use the explicit INNER JOIN).

SELECT apartmentunits.unitid, apartmentunits.name 
FROM apartmentunits, rentconditionsmap
WHERE apartmentunits.aptid = rentconditionsmap.aptid
AND rentconditionsmap.rentcondid = 1
AND rentconditionsmap.condnum = 1
-- AND whatever else...
DVK
ANSI-89 join syntax - Yuck! But I didn't downvote you.
OMG Ponies
Anti-ANSI bigotry! ;)
DVK
+1  A: 

why not:

SELECT unitid, name
FROM apartmentunits a INNER JOIN rentconditionsmap r on a.aptid = r.aptid
WHERE (rentcondid = 1 and condnum = 1) OR (rentcondid = 2 and condnum = 2)

Leslie
SELECT a.unitid, a.name FROM apartmentunits a INNER JOIN rentconditionsmap r ON a.aptid = r.aptid WHERE (r.rentcondid = 1 AND r.condnum = 1) AND (r.rentcondid = 2 AND r.condnum = 1)Tested, and returns no results, where I can see there should be one aptid that matches that set of rentconditions.
Ben Dauphinee
+1  A: 

Using ANSI-92 join syntax:

SELECT au.unitid, 
       au.name 
  FROM APARTMENTUNITS au
  JOIN RENTCONDITIONSMAP rcm ON rcm.aptid = au.aptid
                            AND rcm.rentcondid = 1
                            AND rcm.condnum = 1
OMG Ponies
ANSI-92 syntax is much more readable. The WHERE clause should only have conditions that apply after the JOIN. 1 up
Brent Baisley
+1  A: 

As Eric J said as a comment:

Try changing = to IN

SELECT `unitid`, `name` FROM apartmentunits
WHERE `aptid` IN (
    SELECT `aptid` FROM rentconditionsmap WHERE rentcondid = 1 AND condnum = 1
)
Tor Valamo
Still, that only deals with limiting by one set of rentconditions. I need to be able to query more than one set to limit the apartmentunits returned.
Ben Dauphinee
Sure, but it's _an_ answer, which is why i added it. If you need more, just add OR clauses to the WHERE. `WHERE aptid IN (..) OR aptid IN (..)`
Tor Valamo
Thanks for the clarification. Tested and that seems to work:SELECT `unitid`, `name` FROM apartmentunits WHERE aptid IN (SELECT `aptid` FROM rentconditionsmap WHERE rentcondid = 4 AND condnum = 1) AND aptid IN (SELECT `aptid` FROM rentconditionsmap WHERE rentcondid = 2 AND condnum = 1) ORDER BY name ASC
Ben Dauphinee