zoukankan      html  css  js  c++  java
  • sql语句学习

    1.首先创建表  create table test1 (id int, student_id int,name varchar(20),class_no int,class_name varchar(20),fenshu int);

    2.添加数据:INSERT INTO `test1`(`id`, `student_id`, `name`, `class_no`, `class_name`, `fenshu`) VALUES 

    (1,2005001 ,'张三', 1, '语文', 81),

    (2,2005002 ,'李四', 1, '语文', 81),

    (3,2005001 ,'张三', 1, '语文', 81),

    (4,2005001 ,'张三', 1, '语文', 81);

    添加表,表信息如下:

    自动编号   学号   姓名  课程编号  课程名称  分数
    1      2005001 张三  1   语文    81
    2      2005002 李四  1   语文   81
    3       2005001 张三  1   语文    81
    4       2005001 张三  1   语文    81
     

    问题:删除除自动编号不同,其他都相同的学生冗余信息

    首先我们分析一下问题,只有自动编号不同的,删除其他都相同的数据。

    delete from test1 where id not in (select id from test1 group by  `student_id`, `name`, `class_no`, `class_name`, `fenshu` ); 我想好多人会想到这个办法,可是我们会发现运行不起来总是报错:#1093 - You can't specify target table 'test1' for update in FROM clause

    我们就发现有问题,那我们这么修改呢?

    修改:delete from test1 where id not in  (select bid from (select id as bid from test1 group by  `student_id`, `name`, `class_no`, `class_name`, `fenshu` ) as a);这样子就可以解决你想要的语句了。

  • 相关阅读:
    10月15日模拟赛题解
    NOIp初赛题目整理
    【meet in the mid】【qbxt2019csp刷题班day1C】birthday
    【字符串】 manacher算法
    【border树】【P2375】动物园
    【border相关】【P3426】 [POI2005]SZA-Template
    【字符串】 Z-algorithm
    【字符串】KMP
    【神奇性质】【P5523】D [yLOI2019] 珍珠
    【线段树】【P5522】[yLOI2019] 棠梨煎雪
  • 原文地址:https://www.cnblogs.com/kobigood/p/4475864.html
Copyright © 2011-2022 走看看