qq_41265012 2019-07-02 21:18 采纳率: 70%
浏览 641
已采纳

mysql updata 两个表 sql语句怎么写

表1
id title cid pid
1 a 11 空
2 b 12 空
3 c 11 空
4 d 12 空
5 e 11 空

表2
id pid
11 5
12 6

表1的cid就是表2的id,现在要根据表1的cid的值,通过表2的id与pid对应关系,来更新表1的pid值。
sql语句应该怎么写?

最终结果

表1
id title cid pid
1 a 11 5
2 b 12 6
3 c 11 5
4 d 12 6
5 e 11 5

  • 写回答

3条回答

  • 一切顺势而行 2019-07-03 21:07
    关注

    [SQL]update t1,t2 set t1.pid = t2.pid where t1.cid= t2.id

    受影响的行: 0
    时间: 0.002s

    [Err] 1055 - Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated column 'information_schema.PROFILING.SEQ' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

    BEGIN
    declare c_id int;
    declare p_id int;
    declare done int default FALSE;
    declare v_cur CURSOR for select t2.id as c_id, t2.pid as p_id from t1,t2 where t1.cid = t2.id;
    declare continue HANDLER for not found set done = TRUE;

    open v_cur;
    read_loop:LOOP

        fetch v_cur into  c_id,p_id;
    update t1 set pid = p_id  where  cid = c_id;
    IF done THEN 
        LEAVE read_loop;    
     END IF;
    

    END LOOP read_loop;
    COMMIT;
    CLOSE v_cur;
    END;

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 已采纳回答 7月28日

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘