Page 1 of 1

[SOLVED] Select data where there is no match...

Posted: Tue May 31, 2005 4:24 am
by JayBird
i have 2 tables

Table 1

Code: Select all

CREATE TABLE `gha_files_sections` (                                                                                                                                                 
                      `id` int(10) NOT NULL auto_increment,                                                                                                                                             
                      `section_name` varchar(255) default NULL,                                                                                                                                         
                      PRIMARY KEY  (`id`)                                                                                                                                                               
                    ) ENGINE=InnoDB DEFAULT CHARSET=latin1
and...

Code: Select all

CREATE TABLE `gha_files_subsections` (                                                                                                                                                                                                               
                         `id` int(2) NOT NULL auto_increment,                                                                                                                                                                                                               
                         `subsection_name` varchar(255) default NULL,                                                                                                                                                                                                       
                         `cid` int(2) default NULL,                                                                                                                                                                                                                         
                         `order` int(2) default NULL,                                                                                                                                                                                                                       
                         PRIMARY KEY  (`id`)                                                                                                                                                                                                                                
                       ) ENGINE=InnoDB DEFAULT CHARSET=latin1
Currently i have this query

Code: Select all

SELECT s.id, s.section_name, ss.cid, ss.subsection_name
		FROM ghaintranet.gha_files_sections AS s, ghaintranet.gha_files_subsections AS ss
		WHERE (ss.cid = s.id) OR (s.id = 1)
This selects all the main sections and their sub sections. Problem is, some of the main sections DO NOT have sub sections, so these main sections do not get selected in the query.

Is there some way i can do the query above, but modify it to add main sections that dont have a subsection!?

Or, will i have to have 2 querys?

Posted: Tue May 31, 2005 4:55 am
by timvw
Query should look like (untested)

Code: Select all

SELECT s.id, s.section_name, ss.cid, ss.subsection_name
FROM ghaintranet.gha_files_sections AS s
LEFT OUTER JOIN ghaintranet.gha_files_subsections AS ss
ON s.id = ss.cid

Posted: Tue May 31, 2005 4:59 am
by JayBird
Thanks my man, seems to have worked a treat :D