Mysql update a row with another row value in same table(Mysql 用同一表中的另一行值更新一行)
问题描述
我有一张桌子.我想用同一个表中的第 10 行值更新第 5 行.例如:
I have a table. I want to update the 5th row with 10th row values from the same table. For example:
SlNo   Name Quali   Exp
1        x   B.E     2
2        y   BSC     3
3        Z   B.A     1.5
4        A   MSC     2
5        B   MBA     5
这里我想用第 5 行的值更新第二行.
Here i want to update second row with the value of 5th row.
这是我当前的查询:
    UPDATE table 
      SET Name=(select Name from table where slNo='5'),
               Quali=(select Quali from  table where slNo='5'),
               Exp=(select Exp from table where slNo='5') 
      where slNo='3';
这工作正常......但是如果有超过 20 列,以这种方式编写查询会变得很费力,因为对于每一列,我必须包含另一个子查询......还有其他方法可以编写吗?查询以使用同一表中另一行的所有值更新整行?
this is working fine ... but if there are more than 20 columns it becomes laborious to write a query this way, because for each column I have to include another sub-query... is there any other way to write query to update the whole row with all values from the other row in the same table?
推荐答案
使用多表的自联接 UPDATE 语法:
Use a self-join with the multiple table UPDATE syntax:
UPDATE `table` AS t1 JOIN `table` AS t2 ON t2.slNo = 5
SET    t1.Name = t2.Name, t1.Quali = t2.Quali, t1.Exp = t2.Exp
WHERE  t1.slNo = 3
这篇关于Mysql 用同一表中的另一行值更新一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Mysql 用同一表中的另一行值更新一行
 
				
         
 
            
        基础教程推荐
- 使用 scandir() 在目录中查找文件夹 (PHP) 2022-01-01
- php中的foreach复选框POST 2021-01-01
- Yii2 - 在运行时设置邮件传输参数 2022-01-01
- php 7.4 在写入变量中的 Twig 问题 2022-01-01
- PHPUnit 的 Selenium 2 文档到底在哪里? 2022-01-01
- Web 服务器如何处理请求? 2021-01-01
- 主题化 Drupal 7 的 Ubercart “/cart"页 2021-01-01
- php中的PDF导出 2022-01-01
- 如何在数学上评估像“2-1"这样的字符串?产生“1"? 2022-01-01
- 将变量从树枝传递给 js 2022-01-01
 
    	 
    	 
    	 
    	 
    	 
    	 
    	 
    	 
				 
				 
				 
				