1

我正在尝试在表中插入一行,该表有 3 个外键。为了获得这些键,我有“独特的”要搜索的字段。

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

解决方案

根据 Gordon Linoff 的回答:

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'));

优雅多了!

4

3 回答 3

1

问题似乎是这一行:

FROM item, pharmacy, item

你有item两次。尝试删除一个。

于 2015-06-05T02:02:34.087 回答
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' )
于 2015-06-05T02:04:14.363 回答
1

我认为您想要的查询是:

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');

您的查询有很多错误——同一个表在 from 中多次没有表别名、where子句中的逗号、select在返回多列和(可能)多行而不是一行的标量上下文中。此外,在where子句中使用逗号是非常糟糕的做法。

于 2015-06-05T02:08:28.210 回答