Jump to content

Help with query


imperium2335

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.