I have the following two tables:
system
- id
- systemName
- idOrganization
organization
- id
- officeSymbol
I am running the following query and receiving an id is ambiguous error:
SELECT system.systemName, organization.officeSymbol
FROM system
LEFT JOIN (organization)
ON (system.idOrganization = organization.id)
As you can see, I’m not selecting the id column. If I place system.id within the list of fields to select, I no longer receive this error. Unfortunately, the manner in which this data is handled I can’t return the id – we don’t want it displayed to the user.
Also, if I add GROUP BY system.systemName I no longer get the error – but this just doesn’t seem like the optimal solution.
Note: The LEFT JOIN is intentional as not all systems will be assigned to an Organization.
SELECT VERSION()
--> 5.0.77-community-log
CREATE TABLE system (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`systemName` VARCHAR(45) DEFAULT NULL,
`idOrganization` INT(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `fk_system_organization` (`idOrganization`),
CONSTRAINT `fk_system_organization`
FOREIGN KEY (`idOrganization`)
REFERENCES `organization` (`id`)
ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE organization (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`officeSymbol` VARCHAR(45) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Tested on
5.0.77:SELECT VERSION(); VERSION() 5.0.77 CREATE TABLE organization ( `id` INT(11) NOT NULL AUTO_INCREMENT, `officeSymbol` VARCHAR(45) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1; CREATE TABLE system ( `id` INT(11) NOT NULL AUTO_INCREMENT, `systemName` VARCHAR(45) DEFAULT NULL, `idOrganization` INT(11) DEFAULT NULL, PRIMARY KEY (`id`), KEY `fk_system_organization` (`idOrganization`), CONSTRAINT `fk_system_organization` FOREIGN KEY (`idOrganization`) REFERENCES `organization` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION ) ENGINE=InnoDB DEFAULT CHARSET=latin1; INSERT INTO organization VALUES (1, 'Organization 1'), (2, 'Organization 2'); INSERT INTO system VALUES (1, 'System 1', 1), (2, 'System 2', 2); SELECT system.systemName, organization.officeSymbol FROM system LEFT JOIN (organization) ON (system.idOrganization = organization.id); systemName officeSymbol System 1 Organization 1 System 2 Organization 2Everything works fine.
Note that
LEFT JOINis useless here, since you have aFOREIGN KEYtoorganization, and there will always be anorganizationfor every givensystem.In your comment to
@Artem Barger‘s post you said:Is it that there are other fields in the tables and/or query?
Since you have a syntactic error, every comma may matter.