如何将主表的主键值插入MySQL中子表的外键列?

时间:2016-07-10 17:55:00

标签: mysql database foreign-keys

您好我是PHP和MYSQL的初学者。我做了两张桌子。主表'客户'是:

id | name  |  place  |  email
 1   bob      berlin   bob@bob.com
 2   kary     dresden  kary@kary.com
 3   sam      zurich   sam@sam.com

我的孩子表'命令'其中' menu_id'是外键是:

id  | menu_name | menu_id | date_of_order
 1    sandwich       2       2016-05-03
 2    fruits         3       2016-05-02
 3    pizza          1       2016-05-04
 4    salad          1       2016-05-06

现在通过一个简单的LEFT JOIN我可以看到哪个客户在哪个日期下了哪些订单。

但问题是在我的“客户”中添加新用户的情况。表我无法插入其ID' id'进入' menu_id'列作为外键。

此后假设我

INSERT INTO customers (name, place, email) VALUES ('joe', 'melbourne', 'joe@xyz.com')

在订单表中我做

INSERT INTO orders(menu_name, menu_id, date_of_order)
VALUES('pizza', 'SELECT id FROM customers WHERE email = joe@xyz.com', '2016-05-09')

在此之后'命令'表看起来像

id  | menu_name | menu_id | date_of_order
 1    sandwich       2       2016-05-03
 2    fruits         3       2016-05-02
 3    pizza          1       2016-05-04
 4    salad          1       2016-05-06
 5    pizza          0       2016-05-09

'中的menu_id命令'桌子应该显示' 4'而不是0.我也尝试了LAST_INSERT_ID()但它仍然得到的值是0.我不知道为什么会发生这种情况。请帮忙

1 个答案:

答案 0 :(得分:1)

你的问题是你做错了

INSERT INTO orders(menu_name, menu_id, date_of_order) VALUES('pizza', 'SELECT id FROM customers WHERE email = joe@xyz.com', '2016-05-09')

应该是

INSERT INTO `orders`(`menu_name`, `menu_id`, `date_of_order`) VALUES('pizza', (SELECT `id` FROM `customers` WHERE `email` = 'joe@xyz.com' limit 1), '2016-05-09');

所以你基本上试图插入'SELECT id FROM customers WHERE email = joe@xyz.com'作为转换为int的字符串(如果你把它作为int)

创建声明:

CREATE TABLE `customers` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `name` varchar(245) DEFAULT NULL,
    `place` varchar(245) DEFAULT NULL,
    `email` varchar(245) DEFAULT NULL,
    PRIMARY KEY (`id`)
)  ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;

CREATE TABLE `orders` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `menu_name` varchar(245) DEFAULT NULL,
    `menu_id` int(11) DEFAULT NULL,
    `date_of_order` date DEFAULT NULL,
    PRIMARY KEY (`id`),
    KEY `FK orders menu_id  customer id_idx` (`menu_id`),
    CONSTRAINT `FK orders menu_id  customer id` FOREIGN KEY (`menu_id`)
        REFERENCES `customers` (`id`)
        ON DELETE SET NULL ON UPDATE NO ACTION
)  ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;

你插入:

INSERT INTO `customers` (`name`, `place`, `email`) VALUES ('bob', 'berlin', 'bob@bob.com');
INSERT INTO `customers` (`name`, `place`, `email`) VALUES ('kary', 'dresden', 'kary@kary.com');
INSERT INTO `customers` (`name`, `place`, `email`) VALUES ('sam', 'zurich', 'sam@sam.com');


INSERT INTO `orders`(`menu_name`, `menu_id`, `date_of_order`) VALUES('sandwich', (SELECT id FROM customers WHERE email = 'kary@kary.com' limit 1), '2016-05-09');
INSERT INTO `orders`(`menu_name`, `menu_id`, `date_of_order`) VALUES('fruits', (SELECT id FROM customers WHERE email = 'sam@sam.com' limit 1), '2016-05-09');
INSERT INTO `orders`(`menu_name`, `menu_id`, `date_of_order`) VALUES('pizza', (SELECT id FROM customers WHERE email = 'bob@bob.com' limit 1), '2016-05-09');
INSERT INTO `orders`(`menu_name`, `menu_id`, `date_of_order`) VALUES('salad', (SELECT id FROM customers WHERE email = 'bob@bob.com' limit 1), '2016-05-09');


INSERT INTO `customers` (`name`, `place`, `email`) VALUES ('joe', 'melbourne', 'joe@xyz.com');
INSERT INTO `orders`(`menu_name`, `menu_id`, `date_of_order`) VALUES('pizza', (SELECT id FROM customers WHERE email = 'joe@xyz.com' limit 1), '2016-05-09');

和结果

mysql> select * from customers;
+----+------+-----------+---------------+
| id | name | place     | email         |
+----+------+-----------+---------------+
|  1 | bob  | berlin    | bob@bob.com   |
|  2 | kary | dresden   | kary@kary.com |
|  3 | sam  | zurich    | sam@sam.com   |
|  4 | joe  | melbourne | joe@xyz.com   |
+----+------+-----------+---------------+
4 rows in set (0.02 sec)

mysql> select * from orders;
+----+-----------+---------+---------------+
| id | menu_name | menu_id | date_of_order |
+----+-----------+---------+---------------+
|  1 | sandwich  |       2 | 2016-05-09    |
|  2 | fruits    |       3 | 2016-05-09    |
|  3 | pizza     |       1 | 2016-05-09    |
|  4 | salad     |       1 | 2016-05-09    |
|  5 | pizza     |       4 | 2016-05-09    |
+----+-----------+---------+---------------+
5 rows in set (0.02 sec)