简体   繁体   中英

inner join gives in mysql gives error code 1052

I created a database with following tables :

        CREATE SCHEMA IF NOT EXISTS `facturatiedatabase` ;
        USE `facturatiedatabase` ;

        DROP TABLE IF EXISTS tblAddress ;
        DROP TABLE IF EXISTS tblContact ;
        DROP TABLE IF EXISTS tblCustomers ;

        CREATE TABLE tblCustomers  (
        customerID INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
        firstname VARCHAR(30) NOT NULL,
        lastname VARCHAR(30) NOT NULL,
        vat VARCHAR(30) NOT NULL,
        customerVisible varchar(1) NOT NULL DEFAULT 'T'
        ) ENGINE=InnoDB;


        CREATE TABLE tblContact (
        contactID INT NOT NULL  AUTO_INCREMENT PRIMARY KEY,
        email VARCHAR(100),
        phone VARCHAR(100),
        customerID int,
        CONSTRAINT FK_customerID_Contact FOREIGN KEY (customerID) REFERENCES tblCustomers(customerID) 
        ) ENGINE=InnoDB;

        CREATE TABLE tblAddress (
        addressID INT NOT NULL  AUTO_INCREMENT PRIMARY KEY,
        street VARCHAR(100),
        houseNumber VARCHAR(15),
        city VARCHAR (100),
        country VARCHAR (100),
        customerID int,
        CONSTRAINT FK_customerID_Adress FOREIGN KEY (customerID) REFERENCES tblCustomers(customerID)
        ) ENGINE=InnoDB;


        INSERT INTO tblCustomers (firstname, lastname,vat) 
        VALUES ("John","Doe","UV45856855");

        INSERT INTO tblContact (customerID,phone, email) 
        VALUES ((SELECT DISTINCT LAST_INSERT_ID() FROM tblCustomers), "0000001","Johndoe@gmail.com");

        INSERT INTO tblAddress (customerID,street,housenumber,city,country) 
        VALUES ((SELECT DISTINCT LAST_INSERT_ID() FROM tblCustomers), "berkenlaan","1a","Harelbeke","Belgie");

But when i try following inner join it gives me the following error : LIMIT 0, 1000 Error Code: 1052. Column 'customerID' in field list is ambiguous 0.000 sec.

        SELECT customerID, firstname, lastname, vat,email
        FROM tblCustomers
        INNER JOIN tblContact on tblCustomers.customerID = tblContact.contactID

The error message says it all: the column name customerID is contained in both tables. So which value should mysql select? That is ambiguous, therefore the error.

Have a try with this variant:

SELECT tblCustomers.customerID AS customerID, firstname, lastname, vat,email
    FROM tblCustomers
    INNER JOIN tblContact on tblCustomers.customerID = tblContact.contactID

(or pick that other tables's column with the same if you need that one...)

And, frankly, I doubt your ON clause is the one you want to use. Shouldn't that be ON tblCustomers.customerID = tblContact.customerID instead?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM