MySQL中的多行更新

时间:2019-12-07 06:18:20

标签: mysql

我有两个桌子。

support_table

+------+-------------+
| num  | num_explain |
+------+-------------+
| 1    | 01          |
| 2    | 01          |
| 2    | 02          |
| 3    | 01          |
| 3    | 02          |
| 3    | 03          |
| 4    | 01          |
| 4    | 02          |
| 4    | 03          |
| 4    | 04          |
| 5    | 01          |
| 5    | 02          |
| 5    | 03          |
| 5    | 04          |
| 5    | 05          |
+------+-------------+

class_room

+-----------------+-------------+------+-----+---------+-------+
| Field           | Type        | Null | Key | Default | Extra |
+-----------------+-------------+------+-----+---------+-------+
| seq_no          | varchar(20) | YES  |     | NULL    |       |
| name            | varchar(20) | YES  |     | NULL    |       |
| subjects        | varchar(20) | YES  |     | NULL    |       |
| no_of_student   | varchar(20) | YES  |     | NULL    |       |
| student_roll_no | varchar(20) | YES  |     | NULL    |       |
+-----------------+-------------+------+-----+---------+-------+

现在,我尝试使用以下查询将数据插入表class_room

INSERT INTO class_room (seq_no,name,subjects,no_of_student,student_roll_no)
SELECT '1', 'class11', 'physics', num, num_explain FROM support_table 
WHERE num='3';

此查询对我来说完全正常,它创建了3行。现在该表如下所示:

+---------+---------+----------+---------------+-----------------+
| seq_no  | name    | subjects | no_of_student | student_roll_no |
+---------+---------+----------+---------------+-----------------+
| 1       | class11 | physics  | 3             | 01              |
| 1       | class11 | physics  | 3             | 02              |
| 1       | class11 | physics  | 3             | 03              |
+---------+---------+----------+---------------+-----------------+

现在我想更新此表,因此我尝试了以下代码:

UPDATE class_room 
SET name='class11', subjects='chemistry', no_of_student = 
         (SELECT num_explain FROM support_table WHERE num='4') 
WHERE seq_no='1';

但是此查询表明

  

子查询返回多个行。

在这里,我希望将class_room表中的no_of_student更改为'4',并将student_roll_no更改为'04',而不是3行,而是创建4行。

2 个答案:

答案 0 :(得分:1)

表中有3行,但最终希望得到4行。
这无法通过{strong>不添加新行的UPDATE语句来完成。
执行所需操作的最简单方法是删除当前行,然后插入:

delete from class_room where no_of_student = 3;

insert into class_room (seq_no,name,subjects,no_of_student,student_roll_no) 
select '1', 'class11','chemistry',num,num_explain 
from support_table 
where num='4'; 

请参见demo

| seq_no | name    | subjects  | no_of_student | student_roll_no |
| ------ | ------- | --------- | ------------- | --------------- |
| 1      | class11 | chemistry | 4             | 1               |
| 1      | class11 | chemistry | 4             | 2               |
| 1      | class11 | chemistry | 4             | 3               |
| 1      | class11 | chemistry | 4             | 4               |

答案 1 :(得分:0)

=可以在子查询仅返回1个值时使用。

当子查询返回多个值时,您将不得不使用IN

UPDATE class_room set name='class11',subjects='chemistry',no_of_student IN (select num_explain FROM support_table WHERE num='4')LIMIT 1 WHERE seq_no='1';

示例:

select * 
from table
where id IN (multiple row query);

另一个例子:

SELECT *
FROM Students
WHERE Marks = (SELECT MAX(Marks) FROM Students)   --A Example Subquery returning 1 value

SELECT *
FROM Students
WHERE Marks IN 
      (SELECT Marks 
       FROM Students 
       ORDER BY Marks DESC
       LIMIT 10)                       --Example Subquery returning 10 values
  

@Raging bull

可以很好地解释here