1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
| -- select指定查询哪些列的数据 -- *代表查询所有列 -- from指定查询哪张表 -- DISTINCT可选,指显示结果时,是否去掉重复数据 CREATE TABLE student( id INT NOT NULL DEFAULT 1, NAME VARCHAR(20) NOT NULL DEFAULT '', chinese FLOAT NOT NULL DEFAULT 0.0, english FLOAT NOT NULL DEFAULT 0.0, math FLOAT NOT NULL DEFAULT 0.0 );
INSERT INTO student(id, NAME, chinese, english, math) VALUES(1, '韩顺平', 89, 78, 90); INSERT INTO student(id, NAME, chinese, english, math) VALUES(2, '张飞', 67, 98, 56); INSERT INTO student(id, NAME, chinese, english, math) VALUES(3, '宋江', 87, 78, 77); INSERT INTO student(id, NAME, chinese, english, math) VALUES(4, '关羽', 88, 98, 90); INSERT INTO student(id, NAME, chinese, english, math) VALUES(5, '赵云', 82, 84, 67); INSERT INTO student(id, NAME, chinese, english, math) VALUES(6, '欧阳锋', 55, 85, 45); INSERT INTO student(id, NAME, chinese, english, math) VALUES(7, '黄蓉', 75, 65, 30);
SELECT * FROM student;
-- 查寻表中所有学生的信息 SELECT * FROM student;
-- 查询表中所有学生的姓名和对应的英语成绩 SELECT 'NAME', english FROM student;
-- 过滤表中重复数据distinct -- 要查询的记录每个字段都相同,才会去重 SELECT DISTINCT english FROM student;
-- 统计每个学生的总分 SELECT 'NAME', (chinese + english + math) FROM student;
-- 在所有学生总分加10分 SELECT 'NAME', (chinese + english + math + 10) FROM student;
-- 使用别名表示学生分数 SELECT 'NAME', (chinese + english + math + 10) AS total_score FROM student; SELECT 'NAME' AS '名字', (chinese + english + math + 10) AS total_score FROM student;
-- 查询姓名为赵云的学生成绩 SELECT * FROM student WHERE 'NAME' = '赵云';
-- 查询英语成绩大于90分的同学 SELECT * FROM student WHERE english > 90;
-- 查询总分大于200分的所有同学 SELECT * FROM student WHERE (chinese + english + math) > 200;
-- 查询math大于60并且id大于4的学生成绩 SELECT * FROM student WHERE math > 60 AND id > 4;
-- 查询英语成绩大于语文成绩的同学 SELECT * FROM student WHERE english > chinese;
-- 查询总分大于200分并且数学成绩小于语文成绩的姓韩的学生 -- 韩% 表示名字以韩开头的就可以 SELECT * FROM student WHERE (chinese + english + math) > 200 AND math < chinese AND 'NAME' LIKE '韩%'; -- 查询英语分数在80-90之间的同学 SELECT * FROM student WHERE english >= 80 AND english <= 90; SELECT * FROM student WHERE english BETWWWN 80 AND 90; -- BETWEEN AND 是闭区间
-- 查询数学分数为89,90,91的同学 SELECT * FROM student WHERE math = 89 OR math = 90 OR math = 91; SELECT * FROM student WHERE math IN (89, 90, 91);
|