修改MySQL INSERT语句以省略某些行的插入
我试图对上周我得到帮助的一份声明进行一些扩展。正如您所看到的,我正在设置一个临时表,并插入来自几十所学校最近进行的测试的学生数据行。插入行时,它们按分数排序(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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
沿着这些思路的一些东西可能会起作用。在您的测试环境中,将其更改
为此
但是,如果我是您,我预计排除会成为一个反复出现的问题。也许不频繁,但经常出现。这建议采用基于表的方法——将要排除的内容存储在表中,而不是将它们“存储”在 SQL 语句中。
Something along these lines might work. In your test environment, change this
to this
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.