首先创建一个表格
CREATE TABLE classmate( id INT, cname VARCHAR(5), age INT, math DOUBLE, english DOUBLE, nature DOUBLE );
再给表中填入数据
INSERT INTO classmate VALUES(1,‘王三‘,12,34,55,12); INSERT INTO classmate VALUES(2,‘李四‘,15,67,45,15); INSERT INTO classmate VALUES(3,‘小明‘,20,84,75,50); INSERT INTO classmate VALUES(4,‘顾白‘,23,44,65,52); INSERT INTO classmate VALUES(5,‘铁哥‘,29,88,65,90);
表的展示如下
查询所有的math,并在查询结果中将‘’math‘’改为‘’数学‘’
SELECT math AS ‘数学‘ FROM classmate;
查询各学生的数外总成绩
SELECT cname,(math+english)AS ‘数外总成绩‘ FROM classmate;
查询时去除了重复的英语成绩
SELECT DISTINCT english FROM classmate;
查询名为小明,年龄为20的所有情况
SELECT * FROM classmate WHERE cname = ‘小明‘ AND age =20;
查询数学成绩大于50分或英语成绩大于60分的所有情况
SELECT * FROM classmate WHERE math >50 OR english > 60 ;
查询数学成绩大于等于50分并且小于等于70分的所有情况(使用between and)
SELECT * FROM classmate WHERE math BETWEEN 50 AND 70 ;
查询自然成绩不等于15分的所有情况
SELECT * FROM classmate WHERE nature <> 15;
查询自然成绩不为null的所有情况
SELECT * FROM classmate WHERE nature IS NOT NULL;
查询自然成绩既不为null也不为空字符串的所有情况
SELECT * FROM classmate WHERE nature IS NOT NULL AND nature <> ‘‘;
查询所有姓王的同学的所有情况
SELECT * FROM classmate WHERE cname LIKE ‘王%‘;
查询名字中带白的同学的所有情况
SELECT * FROM classmate WHERE cname LIKE ‘%白%‘;
查询两个字的名字,姓为顾的所有情况
SELECT * FROM classmate WHERE cname LIKE ‘顾_‘;
查询列表中英语的总成绩
SELECT SUM(english) FROM classmate;
查询列表中数学的平均分
SELECT AVG(math) FROM classmate;
查询数学是最高分的同学的姓名
SELECT cname,MAX(math) FROM classmate;
查询自然是最低分的同学的姓名
SELECT cname,MIN(nature) FROM classmate;
查询该班一共有多少个学生
SELECT COUNT(id) FROM classmate;
从第0行开始,查询三行记录(第0行即为实际的第一行,以此类推)
SELECT * FROM classmate LIMIT 0,3;
原文地址:https://www.cnblogs.com/zhangrui0328/p/8989809.html
时间: 2024-10-16 09:33:34