imperium2335 Posted September 14, 2011 Share Posted September 14, 2011 Hi, I keep getting 'Unknown column 'entity_contacts.id' in 'on clause'' in my code: SELECT entity_details.name, entity_contacts.name AS cName, emailT.email, telT.tel, countryTable.country, entity_contacts.id FROM entity_details, entity_contacts, (SELECT countries.country FROM countries, entity_details WHERE countries.id = entity_details.countryRef) AS countryTable LEFT JOIN entity_contacts_emails AS emailT ON emailT.contactRef = entity_contacts.id LEFT JOIN entity_contacts_telephones AS telT ON telT.contactRef = entity_contacts.id WHERE entity_contacts.isPrimary = 1 AND entity_contacts.entityRef = entity_details.id AND entity_details.ownerRef = 1 Where is it wrong? Link to comment https://forums.phpfreaks.com/topic/247102-help-with-query/ Share on other sites More sharing options...
Muddy_Funster Posted September 14, 2011 Share Posted September 14, 2011 moslt likely there is no column called "id" in the entity_contacts table. lets see your table structure if that's not the problem. Link to comment https://forums.phpfreaks.com/topic/247102-help-with-query/#findComment-1269094 Share on other sites More sharing options...
imperium2335 Posted September 14, 2011 Author Share Posted September 14, 2011 CREATE TABLE IF NOT EXISTS `entity_contacts` ( `id` int(11) NOT NULL AUTO_INCREMENT, `entityRef` int(11) NOT NULL, `contactTypeRef` int(2) NOT NULL COMMENT 'Engineer, finance etc', `name` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL, `twitter` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL, `facebook` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL, `linkedIn` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL, `isPrimary` tinyint(1) NOT NULL, PRIMARY KEY (`id`), KEY `customerRef` (`entityRef`), KEY `isPrimary` (`isPrimary`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=7841 ; Link to comment https://forums.phpfreaks.com/topic/247102-help-with-query/#findComment-1269095 Share on other sites More sharing options...
imperium2335 Posted September 14, 2011 Author Share Posted September 14, 2011 I think it's because my JOINs are in the wrong place? Link to comment https://forums.phpfreaks.com/topic/247102-help-with-query/#findComment-1269097 Share on other sites More sharing options...
Muddy_Funster Posted September 14, 2011 Share Posted September 14, 2011 to be honest there is nothing obviously wrong here, All I have done is add some parenthesis and put in an inner join instead of comparing the fields in the WHERE clause, but it looks ok other than that. SELECT entity_details.name, entity_contacts.name AS cName, emailT.email, telT.tel, countryTable.country, entity_contacts.id FROM (SELECT countries.country FROM countries, entity_details WHERE countries.id = entity_details.countryRef) AS countryTable, entity_details INNER JOIN entity_contacts ON (entity_contacts.entityRef = entity_details.id) LEFT JOIN entity_contacts_emails AS emailT ON (emailT.contactRef = entity_contacts.id) LEFT JOIN entity_contacts_telephones AS telT ON (telT.contactRef = entity_contacts.id) WHERE ((entity_contacts.isPrimary = 1) (AND entity_details.ownerRef = 1)) Link to comment https://forums.phpfreaks.com/topic/247102-help-with-query/#findComment-1269110 Share on other sites More sharing options...
imperium2335 Posted September 14, 2011 Author Share Posted September 14, 2011 Thanks, Whats the difference between and INNER JOIN and a LEFT JOIN? Link to comment https://forums.phpfreaks.com/topic/247102-help-with-query/#findComment-1269121 Share on other sites More sharing options...
kickstart Posted September 14, 2011 Share Posted September 14, 2011 Hi INNER JOIN brings back a row when there are matching rows on both tables. LEFT OUTER JOIN brings back a row even if there is no matching row on the right hand table. For example, say you had a table of people, and a table of cars. Using an INNER JOIN you would join the 2 tables together to get a list of people who had cars, with someone who had 2 cars appearing twice in the list but someone who didn't have a car wouldn't be on the list. Using a LEFT OUTER JOIN you could include the people without a car, with the car details being null on the row brought back about them All the best Keith Link to comment https://forums.phpfreaks.com/topic/247102-help-with-query/#findComment-1269129 Share on other sites More sharing options...
imperium2335 Posted September 14, 2011 Author Share Posted September 14, 2011 Thanks. I think I was right in choosing LEFT JOIN then, because I'd want a result to show up even if certain information for a contact is missing, such as telephone etc. Link to comment https://forums.phpfreaks.com/topic/247102-help-with-query/#findComment-1269135 Share on other sites More sharing options...
Muddy_Funster Posted September 14, 2011 Share Posted September 14, 2011 your LEFT JOINS are still there, I added an INNER JOIN to replace the line in your WHERE clause that reads AND entity_contacts.entityRef = entity_details.id Link to comment https://forums.phpfreaks.com/topic/247102-help-with-query/#findComment-1269137 Share on other sites More sharing options...
kickstart Posted September 14, 2011 Share Posted September 14, 2011 I think I was right in choosing LEFT JOIN then, because I'd want a result to show up even if certain information for a contact is missing, such as telephone etc. An INNER JOIN would still bring back a matching row with blank fields. What it won't do (and why you use an OUTER JOIN) is bring back a row when there is no matchig JOINed row. All the best Keith Link to comment https://forums.phpfreaks.com/topic/247102-help-with-query/#findComment-1269214 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.