gpt4 book ai didi

mySQL - 将值插入单列会导致多行吗?

转载 作者:行者123 更新时间:2023-11-29 09:35:38 25 4
gpt4 key购买 nike

我正在将多行插入到单列中。这是表格:

CREATE TABLE Book (
ID INT PRIMARY KEY AUTO_INCREMENT,
Title VARCHAR(100),
Author VARCHAR(40),
Pages INT,
Price FLOAT,
GENRE VARCHAR(30)
);
+----+---------------------------+----------------+-------+--------+-----------+
| ID | Book_Title | Author | Pages | Price | GENRE |
+----+---------------------------+----------------+-------+--------+-----------+
| 1 | The Lost Symbol | Dan Brown | 500 | 524.75 | Fiction |
| 2 | Flamingo | NCERT | 235 | 345.55 | Text Book |
| 3 | The Great Zoo of China | Mathew Reilly | 312 | 401.75 | Fantasy |
| 4 | Walking from east to west | Ravi Zacharias | 240 | 220.45 | Biography |
| 5 | The Time Machine | H.G Wells | 245 | 120.45 | Novel |
| 6 | Flamingo | NCERT | 235 | 345.55 | Text Book |
+----+---------------------------+----------------+-------+--------+-----------+
ALTER TABLE Book ADD COLUMN Year INT;
INSERT INTO Book(Year)
VALUES (1987), (2001), (1997), (1983), (1974), (2001);`

但我得到了完全意想不到的结果:

+----+---------------------------+-------+--------+-----------+------+
| ID | Book_Title | Pages | Price | GENRE | Year |
+----+---------------------------+-------+--------+-----------+------+
| 1 | The Lost Symbol | 500 | 524.75 | Fiction | NULL |
| 2 | Flamingo | 235 | 345.55 | Text Book | NULL |
| 3 | The Great Zoo of China | 312 | 401.75 | Fantasy | NULL |
| 4 | Walking from east to west | 240 | 220.45 | Biography | NULL |
| 5 | The Time Machine | 245 | 120.45 | Novel | NULL |
| 6 | Flamingo | 235 | 345.55 | Text Book | NULL |
| 7 | NULL | NULL | NULL | NULL | 1987 |
| 8 | NULL | NULL | NULL | NULL | 2001 |
| 9 | NULL | NULL | NULL | NULL | 1997 |
| 10 | NULL | NULL | NULL | NULL | 1983 |
| 11 | NULL | NULL | NULL | NULL | 1974 |
| 12 | NULL | NULL | NULL | NULL | 2001 |
+----+---------------------------+-------+--------+-----------+------+

PS:我已经删除了作者

预期结果:

+----+---------------------------+-------+--------+-----------+------+
| ID | Book_Title | Pages | Price | GENRE | Year |
+----+---------------------------+-------+--------+-----------+------+
| 1 | The Lost Symbol | 500 | 524.75 | Fiction | 1987 |
| 2 | Flamingo | 235 | 345.55 | Text Book | 2001 |
| 3 | The Great Zoo of China | 312 | 401.75 | Fantasy | 1997 |
| 4 | Walking from east to west | 240 | 220.45 | Biography | 1983 |
| 5 | The Time Machine | 245 | 120.45 | Novel | 1974 |
| 6 | Flamingo | 235 | 345.55 | Text Book | 2001 |
+----+---------------------------+-------+--------+-----------+------+

我提到了这些但没有用:

服务器版本:5.7.27-0ubuntu0.18.04.1

感谢所有帮助。

最佳答案

使用UPDATE语句而不是INSERT。插入将创建新记录,并且您想要向现有记录添加更多信息。

UPDATE Book SET `Year` = 1987 WHERE ID = 1;
UPDATE Book SET `Year` = 2001 WHERE ID = 2;
UPDATE Book SET `Year` = 1997 WHERE ID = 3;
UPDATE Book SET `Year` = 1983 WHERE ID = 4;
UPDATE Book SET `Year` = 1974 WHERE ID = 5;
UPDATE Book SET `Year` = 2001 WHERE ID = 6;

OR

UPDATE Book
SET `Year` = CASE `ID`
WHEN 1 THEN 1987
WHEN 2 THEN 2001
WHEN 3 THEN 1997
WHEN 4 THEN 1983
WHEN 5 THEN 1974
WHEN 6 THEN 2001
END
WHERE `ID` IN (1,2,3,4,5,6);

如果您不使用 ID 过滤器,建议在 case when 子句中包含 else 部分。

关于mySQL - 将值插入单列会导致多行吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57661343/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com