0

我有一个包含表的数据库。

create table student (
    stu_code integer,
    stu_name varchar(32),
    primary key (stu_code)
)
create table teacher (
    tea_code integer not null,
    tea_name varchar(25),
    primary key (tea_code)
)
create table course (
    cor_code integer not null,
    cor_name varchar(32),
    salary float(32),
    unit integer,
    primary key (cor_code)
)
create table term (
    stu_code integer,
    cor_code integer,
    tea_code integer,
    grade decimal(25),
    year integer,
)

我已经手动填写了三个表格。

术语表是由 Javascript 代码生成的:

<html>
<body>
<div id="rnd"></div>
<script type="text/javascript">
    function rnd(a,b){
        return Math.floor(Math.random()*(b-a+1))+a;
    }
    for (var i=0;i<600;i++) {
        var stu = rnd(41,49),
        cor     = rnd(10,26),
        tea     = rnd(81,90),
        year    = rnd(1391,1392),
        grade   = rnd(8,20);
        (year==1392)?grade = null:true;
        document.writeln(
            "insert into term (stu_code,cor_code,tea_code,grade,year) values "
            +"(884221"              +stu+","
            +"180"                  +cor+","
            +"15120"                +tea+","
            +grade                  +"," 
            +year
            +");"
            +"<br>"
        );
    }
</script>
</body>
</html>

我的问题是术语表中有额外的行。

如何集成/删除其他行?

有没有办法在没有额外随机行的情况下填充表格?

/* 更新 : */

例如 :

insert into term (s,c,t,g) values (45,16,88,16,91);
insert into term (s,c,t,g) values (45,16,86,10,91);
insert into term (s,c,t,g) values (45,16,87,8,91);
insert into term (s,c,t,g) values (41,15,85,20,91);
insert into term (s,c,t,g) values (41,15,88,17,91);

现在我想显示:

insert into term (s,c,t,g) values (45,16,88,16,91);
insert into term (s,c,t,g) values (41,15,85,20,91);
or 

insert into term (s,c,t,g) values (45,16,88,16,91);
insert into term (s,c,t,g) values (41,15,88,17,91);
or

insert into term (s,c,t,g) values (45,16,86,10,91);
insert into term (s,c,t,g) values (41,15,85,20,91);
or 

insert into term (s,c,t,g) values (45,16,86,10,91);
insert into term (s,c,t,g) values (41,15,88,17,91);
or

insert into term (s,c,t,g) values (45,16,87,8,91);
insert into term (s,c,t,g) values (41,15,85,20,91);
or 

insert into term (s,c,t,g) values (45,16,87,8,91);
insert into term (s,c,t,g) values (41,15,88,17,91);

/*结束更新*/

/* 再次更新 */

我可以在 Sql Server 中编写代码 但是这段代码在 Mysql 中不起作用,为什么?Mysql中这段代码的等价物是什么?

delete  from  term
where  stu_code in(
select  stu_code
from  term
group  by  stu_code,cod_code,year
having  COUNT(stu_code)>=2

/* 结束最后一次更新 */

请帮我解决这个问题!

谢谢:x

4

2 回答 2

1

假设每个学生可以报读多门课程,并且每个学生应该只报读一门课程。

由于您只有学生 [41,49] 和课程 [10,26],因此最多有 9 * 17 = 153 种组合。

我会这样做:

<html>
<body>
<div id="rnd"></div>
<script type="text/javascript">
    function rnd(a,b){
        return Math.floor(Math.random()*(b-a+1))+a;
    }
    for (var stu = 41; stu <= 49; stu++) {
        for (var cor = 10; cor <= 26; cor++) {
            tea   = rnd(81,90);
            year  = rnd(1391,1392);
            grade = rnd(8,20);
            (year==1392)?grade = null:true;
            document.writeln(
                "insert into term (stu_code,cor_code,tea_code,grade,year) values "
                +"(884221"              +stu+","
                +"180"                  +cor+","
                +"15120"                +tea+","
                +grade                  +"," 
                +year
                +");"
                +"<br>"
            );
        }
    }
</script>
</body>
</html>

对于您更新的部分

假设您要保留具有最小值的记录year

DELETE a FROM 
    term a JOIN term b 
    ON (a.stu_code = b.stu_code AND a.cod_code = b.cod_code AND a.year > b.year)

所以这个查询的作用是找出并删除所有重复的行,并将较大的行放在year左边。

于 2013-01-29T12:26:31.480 回答
0
delete  from  term
where  stu_code in(
select  stu_code
from  term
group  by  stu_code,cod_code,year
having  COUNT(stu_code)>=2

我认为该查询的问题在于,如果 stu_code >= 2,它将删除所有 stu_code,甚至没有一个副本。如果有多个副本,你想保留 1 个副本。

于 2013-01-29T12:55:25.290 回答