简体   繁体   English

MySQL-选择多个插入的记录

[英]Mysql - select multiple inserted records

How to select multiple inserted records in same stored procedure? 如何在同一存储过程中选择多个插入的记录?

INSERT INTO cars (type, status_id) 
VALUES ('GM',1),
       ('Toyota',2),
       ('Honda',3);

cars has an AUTO_INCREMENT PK field called car_id. 汽车有一个名为car_id的AUTO_INCREMENT PK字段。 let's say the table was inserted 假设表格已插入

1556 GM 1 1556通用1

1557 Toyota 2 1557丰田2

1558 Honda 3 1558本田3

How can I select it? 我该如何选择呢?

Ok, was pretty easy to solve. 好的,很容易解决。

declare new_cars int;
INSERT INTO cars (name, type) 
VALUES  ('GM',1),
        ('Toyota',2),
        ('Honda',3);
select row_count() into new_cars;
select * from cars
order by car_id desc
limit new_cars;

You could use the results of these two functions: 您可以使用以下两个函数的结果:

like this: 像这样:

INSERT INTO cars (type, status_id) 
VALUES ('GM',1),
       ('Toyota',2),
       ('Honda',3);

SELECT *
FROM cars
WHERE car_id BETWEEN LAST_INSERT_ID()
                 AND LAST_INSERT_ID() + ROW_COUNT() - 1
;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM