如何在MYSQL中插入INTO SELECT语句?

时间:2020-06-24 13:29:59

标签: mysql select insert

我需要在数据库中插入有关当天已预订航班的乘客的新预订信息。我一直在这个问题上徘徊了很长时间,但成效有限。这是我到目前为止的位置:

INSERT INTO BOOKING(name, DOB, flightNumber, departAirport, departTime, class, bookingTime)
SELECT * 
FROM BOOKING 
WHERE departTime = '2019/05/05 06:05';  

我知道,就问题要我做的事情而言,这是不正确的。我在这里设置了一个小提琴以帮助更好地理解我的问题:https://www.db-fiddle.com/f/ngtB4SPcuK4PfMG3sQhwtP/0

相同的数据


INSERT INTO SEATING VALUES ('VA815', 'MEL', STR_TO_DATE('11/05/2019 07:30', '%d/%m/%Y %H:%i'), 'BUSINESS', 0);
INSERT INTO SEATING VALUES ('VA815', 'MEL', STR_TO_DATE('11/05/2019 07:30', '%d/%m/%Y %H:%i'), 'ECONOMY', 0);
INSERT INTO SEATING VALUES ('CA3101', 'SYD', STR_TO_DATE('26/05/2019 22:15', '%d/%m/%Y %H:%i'), 'BUSINESS', 45);
INSERT INTO SEATING VALUES ('CA3101', 'SYD', STR_TO_DATE('26/05/2019 22:15', '%d/%m/%Y %H:%i'), 'ECONOMY', 320);
INSERT INTO SEATING VALUES ('CA3101', 'SYD', STR_TO_DATE('26/05/2019 22:15', '%d/%m/%Y %H:%i'), 'FIRST', 8);
INSERT INTO SEATING VALUES ('QF129', 'SYD', STR_TO_DATE('26/05/2019 09:35', '%d/%m/%Y %H:%i'), 'BUSINESS', 45);
INSERT INTO SEATING VALUES ('QF129', 'SYD', STR_TO_DATE('26/05/2019 09:35', '%d/%m/%Y %H:%i'), 'ECONOMY', 150);
INSERT INTO SEATING VALUES ('QF129', 'SYD', STR_TO_DATE('26/05/2019 09:35', '%d/%m/%Y %H:%i'), 'FIRST', 14);
INSERT INTO SEATING VALUES ('QF4014', 'PEK', STR_TO_DATE('10/05/2019 07:35', '%d/%m/%Y %H:%i'), 'BUSINESS', 18);
INSERT INTO SEATING VALUES ('QF4014', 'PEK', STR_TO_DATE('10/05/2019 07:35', '%d/%m/%Y %H:%i'), 'ECONOMY', 22);
INSERT INTO SEATING VALUES ('QF4014', 'PEK', STR_TO_DATE('10/05/2019 07:35', '%d/%m/%Y %H:%i'), 'FIRST', 5);
INSERT INTO SEATING VALUES ('QF497', 'SYD', STR_TO_DATE('26/05/2019 22:05', '%d/%m/%Y %H:%i'), 'BUSINESS', 16);
INSERT INTO SEATING VALUES ('QF497', 'SYD', STR_TO_DATE('26/05/2019 22:05', '%d/%m/%Y %H:%i'), 'ECONOMY', 25);
INSERT INTO SEATING VALUES ('QF497', 'SYD', STR_TO_DATE('26/05/2019 22:05', '%d/%m/%Y %H:%i'), 'FIRST', 3);
INSERT INTO SEATING VALUES ('CA2842', 'PEK', STR_TO_DATE('10/05/2019 22:05', '%d/%m/%Y %H:%i'), 'BUSINESS', 1);
INSERT INTO SEATING VALUES ('CA2842', 'PEK', STR_TO_DATE('10/05/2019 22:05', '%d/%m/%Y %H:%i'), 'ECONOMY', 10);
INSERT INTO SEATING VALUES ('CA2842', 'PEK', STR_TO_DATE('10/05/2019 22:05', '%d/%m/%Y %H:%i'), 'FIRST', 0);



INSERT INTO BOOKING VALUES ('Mike', STR_TO_DATE('10/08/1982', '%d/%m/%Y'), 'QF8764', 'SYD', STR_TO_DATE('10/05/2019 08:45', '%d/%m/%Y %H:%i'), 'ECONOMY', STR_TO_DATE('1/05/2019 10:35', '%d/%m/%Y %H:%i'));
INSERT INTO BOOKING VALUES ('Mike', STR_TO_DATE('10/08/1982', '%d/%m/%Y'), 'QF322', 'AKL', STR_TO_DATE('18/05/2019 06:00', '%d/%m/%Y %H:%i'), 'ECONOMY', STR_TO_DATE('1/05/2019 10:35', '%d/%m/%Y %H:%i'));
INSERT INTO BOOKING VALUES ('Jean', STR_TO_DATE('12/05/1988', '%d/%m/%Y'), 'JQ402', 'SYD', STR_TO_DATE('23/04/2019 07:05', '%d/%m/%Y %H:%i'), 'ECONOMY', STR_TO_DATE('1/04/2019 12:05', '%d/%m/%Y %H:%i'));
INSERT INTO BOOKING VALUES ('Alex', STR_TO_DATE('21/07/1992', '%d/%m/%Y'), 'VA505', 'SYD', STR_TO_DATE('24/04/2019 07:10', '%d/%m/%Y %H:%i'), 'BUSINESS', STR_TO_DATE('10/04/2019 09:35', '%d/%m/%Y %H:%i'));
INSERT INTO BOOKING VALUES ('Alex', STR_TO_DATE('21/07/1992', '%d/%m/%Y'), 'VA523', 'SYD', STR_TO_DATE('26/04/2019 13:10', '%d/%m/%Y %H:%i'), 'BUSINESS', STR_TO_DATE('10/04/2019 09:35', '%d/%m/%Y %H:%i'));
INSERT INTO BOOKING VALUES ('Ben', STR_TO_DATE('22/04/1994', '%d/%m/%Y'), 'QF8764', 'SYD', STR_TO_DATE('10/05/2019 08:45', '%d/%m/%Y %H:%i'), 'ECONOMY', STR_TO_DATE('25/04/2019 11:35', '%d/%m/%Y %H:%i'));
INSERT INTO BOOKING VALUES ('Ben', STR_TO_DATE('22/04/1994', '%d/%m/%Y'), 'QF322', 'AKL', STR_TO_DATE('18/05/2019 06:00', '%d/%m/%Y %H:%i'), 'ECONOMY', STR_TO_DATE('27/04/2019 16:15', '%d/%m/%Y %H:%i'));
INSERT INTO BOOKING VALUES ('Ben', STR_TO_DATE('22/04/1994', '%d/%m/%Y'), 'JQ402', 'SYD', STR_TO_DATE('23/04/2019 07:05', '%d/%m/%Y %H:%i'), 'ECONOMY', STR_TO_DATE('17/04/2019 14:30', '%d/%m/%Y %H:%i'));
INSERT INTO BOOKING VALUES ('Duke', STR_TO_DATE('28/03/1974', '%d/%m/%Y'), 'VA505', 'SYD', STR_TO_DATE('24/04/2019 07:10', '%d/%m/%Y %H:%i'), 'BUSINESS', STR_TO_DATE('20/04/2019 13:30', '%d/%m/%Y %H:%i'));
INSERT INTO BOOKING VALUES ('Josef', STR_TO_DATE('27/11/1982', '%d/%m/%Y'), 'QF401', 'SYD', STR_TO_DATE('05/05/2019 06:05', '%d/%m/%Y %H:%i'), 'ECONOMY', STR_TO_DATE('22/04/2019 10:30', '%d/%m/%Y %H:%i'));
INSERT INTO BOOKING VALUES ('Harry', STR_TO_DATE('16/07/1990', '%d/%m/%Y'), 'QF860', 'SYD', STR_TO_DATE('23/04/2019 07:10', '%d/%m/%Y %H:%i'), 'FIRST', STR_TO_DATE('21/04/2019 10:30', '%d/%m/%Y %H:%i'));
INSERT INTO BOOKING VALUES ('Will', STR_TO_DATE('20/10/1977', '%d/%m/%Y'), 'QF8764', 'SYD', STR_TO_DATE('10/05/2019 08:45', '%d/%m/%Y %H:%i'), 'BUSINESS', STR_TO_DATE('25/04/2019 15:30', '%d/%m/%Y %H:%i'));
INSERT INTO BOOKING VALUES ('Douglas', STR_TO_DATE('20/11/1967', '%d/%m/%Y'), 'QF497', 'SYD', STR_TO_DATE('26/05/2019 22:05', '%d/%m/%Y %H:%i'), 'ECONOMY', STR_TO_DATE('21/04/2019 12:30', '%d/%m/%Y %H:%i'));
INSERT INTO BOOKING VALUES ('Douglas', STR_TO_DATE('20/11/1967', '%d/%m/%Y'), 'CA2842', 'PEK', STR_TO_DATE('10/05/2019 22:05', '%d/%m/%Y %H:%i'), 'ECONOMY', STR_TO_DATE('21/04/2019 12:30', '%d/%m/%Y %H:%i'));

这是我收到的错误,因为数据不是像问题所要求的那样新,而是与另一条插入数据相同:

Query Error: Error: ER_DUP_ENTRY: Duplicate entry 'Josef-1982-11-27-QF401-SYD-2019-05-05 06:05:00-ECONOMY-2019-04-2' for key 'PRIMARY'

1 个答案:

答案 0 :(得分:1)

您已经在数据库中添加了该条目,并且主键包含所有字段,因此您不能在该数据库中添加相同的内容。