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日

悬赏问题

  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 spring后端vue前端
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题