我尝试使用内部联接创建过程。我一直听到以下错误:
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 ;
答案 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
声明中删除owns
和FROM
。
如果您想要两者,则必须为car
和owns
添加别名:
FROM car AS car1, clients, owns AS owns1
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();