2015-06-05 146 views
1

我試圖插入一行到表中,該表有3個外鍵。要獲得這些密鑰,我有「獨特」的搜索文件。MySQL插入到選擇

SQL

create table user(
    id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, 
    name VARCHAR(30) NOT NULL, 
    username VARCHAR(50) NOT NULL, 
    password VARCHAR(30) NOT NULL, 
    access_token VARCHAR(32), 
    reg_date TIMESTAMP, 
    UNIQUE(username), 
    UNIQUE(access_token) 


); 

create table pharmacy(
    id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, 
    places_id VARCHAR(50) NOT NULL, 
    name VARCHAR(30) NOT NULL, 
    coord VARCHAR(50) NOT NULL, 
    description VARCHAR(100), 
    reg_date TIMESTAMP, 
    UNIQUE(places_id) 
); 

create table item(
    id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, 
    name VARCHAR(30) NOT NULL, 
    description VARCHAR(100), 
    reg_date TIMESTAMP, 
    UNIQUE(name) 
); 

create table item_bought(
    id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, 
    id_user INT(6) UNSIGNED, 
    id_pharmacy INT(6) UNSIGNED, 
    id_item INT(6) UNSIGNED, 
    price float(6,2) NOT NULL, 
    bought_date date, 
    reg_date TIMESTAMP, 

    foreign key(id_user)  
     references user(id), 

    foreign key(id_pharmacy) 
     references pharmacy(id), 

    foreign key(id_item) 
     references item(id) 
); 

查詢我建立在JAVA:

INSERT INTO item_bought (id_user, id_pharmacy, id_item, price, bought_date) 
    SELECT user.id, pharmacy.id, item.id, 12.32, date('1999-02-24') 
    FROM user, pharmacy, item 
    WHERE (SELECT user.id, user.username, pharmacy.id, 
    pharmacy.places_id, item.id, item.name 
     FROM item, pharmacy, item 
     WHERE user.username='jhon', pharmacy.places_id='id1', item.name='ibuprufen') 

這是我得到的錯誤:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Not unique table/alias: 'item' 

感謝您的幫助:d

解決方案

基於戈登·利諾夫的答案:

INSERT INTO item_bought (id_user, id_pharmacy, id_item, price, bought_date) 
    SELECT (SELECT u.id FROM user u WHERE u.username = 'jhon'), 
      (SELECT p.id FROM pharmacy p WHERE p.places_id = 'id1'), 
      (SELECT i.id FROM item i WHERE i.name='ibuprufen'), 
      (SELECT 13.22), (SELECT date('1999-02-12')); 

更優雅!

+0

你想做什麼?您似乎不太可能希望將用戶,藥房和物品的完整笛卡爾產品插入表格中。示例數據和所需結果非常有用,特別是當查詢格式錯誤時。請注意:您的特定語法問題是'WHERE'子句中的逗號。 –

+0

您是否嘗試過使用連接? – aizele

回答

2

我想你想查詢的是:

INSERT INTO item_bought (id_user, id_pharmacy, id_item, price, bought_date) 
    SELECT (SELECT u.id FROM user u WHERE u.username = 'jhon'), 
      (SELECT p.id FROM pharmacy p WHERE p.places_id = 'id1'), 
      (SELECT i.id FROM item i WHERE i.name='ibuprufen'); 

你的查詢有許多錯誤 - 在同一個表多次從沒有表的別名,逗號在where子句,標量上下文中的select,該標量上下文返回多個列和(可能)多個行而不是一個。另外,在where條款中使用逗號是非常糟糕的做法。

+0

謝謝你,非常優雅的解決方案,只需要添加2個選擇與價格和buying_date。我真的搜索了很多,找到一個很好的解決方案,從來沒有找到像這樣的東西。謝謝哦,StackOverflow的騎士! :d – CiberWizZ

1

的問題似乎是這一行:

FROM item, pharmacy, item 

你有item兩次。嘗試刪除一個。

1

在您的代碼中,您正在同一個查詢中調用兩次同一個表。這是錯誤的。它應該是:

INSERT INTO item_bought (id_user, id_pharmacy, id_item, price, bought_date) 
    SELECT user.id, pharmacy.id, item.id, 12.32, date('1999-02-24') 
    FROM user, pharmacy, item 
    WHERE (SELECT user.id, user.username, pharmacy.id, 
    pharmacy.places_id, item.id, item.name 
     FROM item, pharmacy 
     WHERE user.username='jhon', pharmacy.places_id='id1', item.name='ibuprufen')