当前位置:首页 > 从零开始 > 正文

mysql查询语句问题

2023-09-25 03:18:28  来源:网络   热度:

一、mysql查询语句问题

select count(学号) from student where grade>(select grade from student where 学号=?)

这样查出来的就是比该学生成绩好的学生的人数,再对查出的结果加一就是这个学生的名次了

没有表结构,没有关联怎么查出成绩排名,还要加入学生姓名,无能为力。

SELECT username FROM stedent ORDER BY grade DESC

二、MYSQL数据库如何执行SQL语句

如果你是在命令提示符下键入SQL语句,结尾需要加分号,回车即可,如果你是在MYSQL的一个集成开发环境下操作,将SQL语句键入页面上方的一个框里,然后按菜单栏上的三角箭头

select @a=DRClass1, @b=DRClass2, @c=DRClass3, @d=DRClass4, @e=DRClass5 from Teacher Where TeacherID = @TeacherID create table classname(classname char(50)) insert into classname (classname) values (@a) if (@b is not null) begin insert into classname (classname) values (@b) if (@c is not null) begin insert into classname (classname) values (@c) if (@d is not null) begin insert into classname (classname) values (@d) if (@e is not null) begin insert into classname (classname) values (@e) end end end end select * from classname 以上这些SQL语句能不能转成一个存储过程?我自己试了下 ALTER PROCEDURE Pr_GetClass @TeacherID int, @a char(50), @b char(50), @c char(50), @d char(50), @e char(50) as select @a=DRClass1, @b=DRClass2, @c=DRClass3, @d=DRClass4, @e=DRClass5 from Teacher Where TeacherID = @TeacherID DROP TABLE classname create table classname(classname char(50)) insert into classname (classname) values (@a) if (@b is not null) begin insert into classname (classname) values (@b) if (@c is not null) begin insert into classname (classname) values (@c) if (@d is not null) begin insert into classname (classname) values (@d) if (@e is not null) begin insert into classname (classname) values (@e) end end end end select * from classname 但是这样的话,这个存储过程就有6个变量,实际上应该只提供一个变量就可以了 主要的问题就是自己没搞清楚 @a,@b,@C,@d 等是临时变量,是放在as后面重新做一些申明的,而不是放在开头整个存储过程的变量定义。 (标准化越来越近了):namespace prefix = o ns = urn:schemas-microsoft-com:office:office /> 实战SQL语句收集(不断更新中--) 前言:这里将我编程实践中遇到的有价值的sql语句一路记下来,一方面方便自己查用,一方面也夯实下即将遗忘的回忆。整个过程中我会不断更新,直到不能再加为止,同时,这里只记录最实用的咚咚,不效仿学院派的那一套。

很简单的,不懂,问我。

三、求解mysql

下面是创建函数的示例代码:

DELIMITER $$

CREATE DEFINER=`root`@`localhost` FUNCTION `Fun_avg`(var_xs_id varchar(12), var_xn int) RETURNS float

BEGIN

DECLARE var_r float;

SELECT AVG(tc_score) INTO var_r

FROM tc_scores

WHERE xs_id = var_xs_id AND xn = var_xn;

RETURN var_r;

END$$

DELIMITER ;

在上面的代码中,我们首先声明了一个返回值类型为 float 的变量 var_r。然后,我们使用 SELECT 语句查询 tc_scores 表中指定学号 var_xs_id 和指定学年 var_xn 的体测成绩的平均值,并将该平均值赋给 var_r。最后,我们使用 RETURN 语句将 var_r 的值返回给函数的调用者。

注意:在 MySQL 中,必须使用 DELIMITER 语句来改变默认的分号作为语句结束符的行为。因此,我们在创建函数时必须使用 DELIMITER 语句来指定自定义的分隔符(在本例中为 $$),并在函数结束后再次使用 DELIMITER 语句将分隔符恢复为默认值(即分号)。

一周热门