内部联接"不是唯一的表"

时间:2014-06-17 11:49:07

标签: mysql sql

我尝试使用内部联接创建过程。我一直听到以下错误:

Not unique table/alias: 'car'

我做错了什么?

我的程序

DELIMITER //   
CREATE PROCEDURE orderCreate(IN _LicenseNumber varchar(30), _Phonenumber varchar(20))
BEGIN    
INSERT INTO `order` (carID, clientID) 
Select car.id, client.id 
FROM car, client, owns   
INNER JOIN car 
ON car.id = owns.carID

INNER JOIN owns 
ON owns.clientID = client.id;

WHERE  car.licensenumber = _LicenseNumber 
AND client.phonenumber = _Phonenumber 
SELECT LAST_INSERT_ID(); 

END // 
DELIMITER ;  

2 个答案:

答案 0 :(得分:0)

您正在同时使用INNER JOIN和笛卡尔积(MySQL 1 中的相同):

FROM car, client, owns   
INNER JOIN car 
ON car.id = owns.carID

INNER JOIN owns 
ON owns.clientID = client.id;

只需从car声明中删除ownsFROM

如果您想要两者,则必须为carowns添加别名:

FROM car AS car1, clients, owns AS owns1

参考

  1. http://dev.mysql.com/doc/refman/4.1/en/join.html
  2.   

    INNER JOIN和(逗号)在没有的情况下在语义上是等价的   连接条件:两者之间产生笛卡尔积   指定的表格

答案 1 :(得分:0)

我认为你有很多桌子: 尝试:

INSERT INTO `order` (carID, clientID) 
Select car.id, client.id 
FROM car    
INNER JOIN owns 
ON owns.clientID = client.id;
WHERE  car.licensenumber = _LicenseNumber 
AND client.phonenumber = _Phonenumber 
SELECT LAST_INSERT_ID();