修改MySQL INSERT语句以省略某些行的插入

发布于 2024-10-12 01:17:52 字数 1175 浏览 4 评论 0原文

我试图对上周我得到帮助的一份声明进行一些扩展。正如您所看到的,我正在设置一个临时表,并插入来自几十所学校最近进行的测试的学生数据行。插入行时,它们按分数排序(totpct_stu,从高到低),并添加 row_number,其中 1 代表最高分,等等。

我了解到学校 #9999 在 SMITH 的班级中存在一些问题(每个学生都取得了满分,他们是该学区唯一获得满分的学生)。所以,我不想导入 SMITH 的课程。

正如您所看到的,我删除了 SMITH 的课程,但这搞乱了学校其余学生的行编号(例如,高分 row_number 现在是 20,而不是 1)。

如何修改 INSERT 语句以便不插入此类?

DROP TEMPORARY TABLE IF EXISTS avgpct ;
CREATE TEMPORARY TABLE avgpct_1
   ( sch_code    VARCHAR(3),
     schabbrev   VARCHAR(75),
     teachername VARCHAR(75),
     totpct_stu  DECIMAL(5,1),
     row_number  SMALLINT,
     dummy       VARCHAR(75)
   );
-- ----------------------------------------
INSERT INTO avgpct
SELECT sch_code
     , schabbrev
     , teachername
     , totpct_stu
     , @num := IF( @GROUP = schabbrev, @num + 1, 1 ) AS row_number
     , @GROUP := schabbrev AS dummy
  FROM sci_rpt
 WHERE grade = '05' AND 
       totpct_stu >= 1   -- has a valid score
 ORDER 
    BY sch_code, totpct_stu DESC ;
-- ---------------------------------------
-- select * from avgpct ;
-- ---------------------------------------
DELETE 
  FROM avgpct_1
 WHERE sch_code = '9999' AND 
       teachername = 'SMITH' ;

I'm trying to expand a little on a statement that I received help with last week. As you can see, I'm setting up a temporary table and inserting rows of student data from a recently administered test for a few dozen schools. When the rows are inserted, they are sorted by the score (totpct_stu, high to low) and the row_number is added, with 1 representing the highest score, etc.

I've learned that there were some problems at school #9999 in SMITH's class (every student made a perfect score and they were the only students in the district to do so). So, I do not want to import SMITH's class.

As you can see, I DELETED SMITH's class, but this messed up the row numbering for the remainder of student at the school (e.g., high score row_number is now 20, not 1).

How can I modify the INSERT statement so as to not insert this class?

DROP TEMPORARY TABLE IF EXISTS avgpct ;
CREATE TEMPORARY TABLE avgpct_1
   ( sch_code    VARCHAR(3),
     schabbrev   VARCHAR(75),
     teachername VARCHAR(75),
     totpct_stu  DECIMAL(5,1),
     row_number  SMALLINT,
     dummy       VARCHAR(75)
   );
-- ----------------------------------------
INSERT INTO avgpct
SELECT sch_code
     , schabbrev
     , teachername
     , totpct_stu
     , @num := IF( @GROUP = schabbrev, @num + 1, 1 ) AS row_number
     , @GROUP := schabbrev AS dummy
  FROM sci_rpt
 WHERE grade = '05' AND 
       totpct_stu >= 1   -- has a valid score
 ORDER 
    BY sch_code, totpct_stu DESC ;
-- ---------------------------------------
-- select * from avgpct ;
-- ---------------------------------------
DELETE 
  FROM avgpct_1
 WHERE sch_code = '9999' AND 
       teachername = 'SMITH' ;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

慕烟庭风 2024-10-19 01:17:52
INSERT INTO avgpct
SELECT sch_code
     , schabbrev
     , teachername
     , totpct_stu
     , @num := IF( @GROUP = schabbrev, @num + 1, 1 ) AS row_number
     , @GROUP := schabbrev AS dummy
  FROM sci_rpt
 WHERE grade = '05' AND 
       totpct_stu >= 1   -- has a valid score
       AND NOT (sch_code = '9999' AND 
       teachername = 'SMITH')
 ORDER 
    BY sch_code, totpct_stu DESC ;
INSERT INTO avgpct
SELECT sch_code
     , schabbrev
     , teachername
     , totpct_stu
     , @num := IF( @GROUP = schabbrev, @num + 1, 1 ) AS row_number
     , @GROUP := schabbrev AS dummy
  FROM sci_rpt
 WHERE grade = '05' AND 
       totpct_stu >= 1   -- has a valid score
       AND NOT (sch_code = '9999' AND 
       teachername = 'SMITH')
 ORDER 
    BY sch_code, totpct_stu DESC ;
夜吻♂芭芘 2024-10-19 01:17:52

沿着这些思路的一些东西可能会起作用。在您的测试环境中,将其更改

WHERE grade = '05' AND 
       totpct_stu >= 1   -- has a valid score

为此

WHERE grade = '05' AND 
       totpct_stu >= 1  AND NOT -- has a valid score
       (sch_code = '9999' AND teachername = 'SMITH')

但是,如果我是您,我预计排除会成为一个反复出现的问题。也许不频繁,但经常出现。这建议采用基于表的方法——将要排除的内容存储在表中,而不是将它们“存储”在 SQL 语句中。

Something along these lines might work. In your test environment, change this

WHERE grade = '05' AND 
       totpct_stu >= 1   -- has a valid score

to this

WHERE grade = '05' AND 
       totpct_stu >= 1  AND NOT -- has a valid score
       (sch_code = '9999' AND teachername = 'SMITH')

But, if I were you, I'd expect exclusions to be a recurring problem. Maybe not frequent, but regularly recurring. That would suggest a table-based approach--storing things to be excluded in a table, rather than "storing" them in the SQL statements.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文