will35010 Posted August 2, 2010 Share Posted August 2, 2010 I'm trying to create a select query that returns a list of rooms that aren't being used. I need help creating a query that shows me the rooms.room that aren't being used in visit_data.room. When a room is being occupied, my script puts the value from rooms.room into visit_data.room. Any help would be greatly appreciated!!! -- -- Table structure for table `rooms` -- CREATE TABLE IF NOT EXISTS `rooms` ( `id` int(11) NOT NULL AUTO_INCREMENT, `areaid` varchar(50) NOT NULL, `room` varchar(50) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=10 ; INSERT INTO `rooms` (`id`, `areaid`, `room`) VALUES (1, 'ED', 'TA'), (7, 'ED', 'TB'), (9, 'ED', 'T2'); -- -- Table structure for table `visit_data` -- CREATE TABLE IF NOT EXISTS `visit_data` ( `visitid` int(11) NOT NULL AUTO_INCREMENT, `patientid` varchar(45) NOT NULL, `priority` varchar(100) NOT NULL, `alert1` varchar(100) DEFAULT '1', `alert2` varchar(100) DEFAULT '1', `alert3` varchar(100) DEFAULT '1', `cc` varchar(50) NOT NULL, `walkout` varchar(3) DEFAULT NULL, `areaid` varchar(45) NOT NULL, `room` varchar(45) DEFAULT NULL, `current_status` varchar(45) DEFAULT NULL, `doctor` varchar(50) DEFAULT NULL, `nurse` varchar(50) DEFAULT NULL, `reg_time` varchar(12) DEFAULT NULL, `discharged` varchar(45) DEFAULT NULL, `discharged_time` varchar(12) DEFAULT NULL, `disposition` varchar(100) DEFAULT NULL, PRIMARY KEY (`visitid`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ; Quote Link to comment https://forums.phpfreaks.com/topic/209610-query-help/ Share on other sites More sharing options...
Maq Posted August 2, 2010 Share Posted August 2, 2010 How do you know when a room is occupied? Also, what is your FK that relates 'visit_data' and 'rooms'? Quote Link to comment https://forums.phpfreaks.com/topic/209610-query-help/#findComment-1094338 Share on other sites More sharing options...
brianlange Posted August 2, 2010 Share Posted August 2, 2010 You can use a left join and then in the where clause specify that the value for the column in the visit_data table is null SELECT * FROM `rooms` LEFT JOIN visit_data ON rooms.room = visit_data.room WHERE visit_data.room IS NULL Quote Link to comment https://forums.phpfreaks.com/topic/209610-query-help/#findComment-1094350 Share on other sites More sharing options...
will35010 Posted August 2, 2010 Author Share Posted August 2, 2010 Thank you for actually reading my question. Your solution worked. Thanks!!! You can use a left join and then in the where clause specify that the value for the column in the visit_data table is null SELECT * FROM `rooms` LEFT JOIN visit_data ON rooms.room = visit_data.room WHERE visit_data.room IS NULL Quote Link to comment https://forums.phpfreaks.com/topic/209610-query-help/#findComment-1094372 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.