给定书号 id和书本数,我想在表格中插入与书本一样多的行。这是我的非工作解决方案:
DROP PROCEDURE IF EXISTS insert_into_book_copy_table;
CREATE PROCEDURE insert_into_book_copy_table (IN in_book_id INT,
IN in_num INT)
BEGIN
SET @I = 1;
SET @ACQUIRED = CURRENT_TIMESTAMP;
WHILE I <= in_num DO
INSERT INTO book_copy (book_id, acquired) # The table has an
VALUES (in_book_id, @ACQUIRED); # auto-increment field
# which I didn't list here
SET @I = @I + 1;
END WHILE;
END$$
例如,如果给定的书号 id是 23 并且书本的数量等于 8,那么book_copy 表应该如下所示:
SELECT * FROM book_copy;
╔══════════════╦══════════╦═════════════════════╗
║ book_copy_id ║ book_id ║ acquired ║
╠══════════════╬══════════╬═════════════════════╣
║ 1 ║ 23 ║ 2013-04-15 18:15:20 ║
║ 2 ║ 23 ║ 2013-04-15 18:15:20 ║
║ 3 ║ 23 ║ 2013-04-15 18:15:20 ║
║ 4 ║ 23 ║ 2013-04-15 18:15:20 ║
║ 5 ║ 23 ║ 2013-04-15 18:15:20 ║
║ 6 ║ 23 ║ 2013-04-15 18:15:20 ║
║ 7 ║ 23 ║ 2013-04-15 18:15:20 ║
║ 8 ║ 23 ║ 2013-04-15 18:15:20 ║
╚══════════════╩══════════╩═════════════════════╝
有没有更好的编码方式?