create table test01(id tinyint);CREATE TABLE test02(id tinyint unsigned);insert into test01 values(-128);insert into test02 values(128);select * from test01;select * from test02;
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,'曹志',99,99,99);INSERT INTO student (id,`name`,chinese,English,Math)VALUES(2,'张三',89,78,86);INSERT INTO student (id,`name`,chinese,English,Math)VALUES(3,'李四',76,87,56);INSERT INTO student (id,`name`,chinese,English,Math)VALUES(4,'王五',87,56,76);INSERT INTO student (id,`name`,chinese,English,Math)VALUES(5,'熊大',81,83,80);INSERT INTO student (id,`name`,chinese,English,Math)VALUES(6,'熊二',71,72,73);INSERT INTO student (id,`name`,chinese,English,Math)VALUES(7,'Tom',7,74,75);
select:
# 查询表中所有学生的信息select * from student;# 查询表中所有学生的姓名和对应的英语成绩select `name`,English from student;# 过滤表中重复数据 distinctselect distinct * from student;
使用表达式对查询的列进行运算
在select语句中可使用as语句
# 统计每个学生的总分SELECT `name`, (English+Math+chinese) FROM student;# 在所有学生总分加十分的情况SELECT `name`, (English + Math + chinese + 10) FROM student;# 使用别名表示学生分数SELECT `name`, (English + Math + chinese + 10) AS `total` FROM student;
# 查询姓名为赵云的学生成绩SELECT * FROM student WHERE `name` = '张三';# 查询英语成绩大于90分的同学SELECT * FROM student WHERE English > 90;# 查询总分大于200分的所有同学SELECT * FROM student WHERE (English+Math+chinese) > 200;
# 查询math大于60 并且(AND) id大于90的学生成绩SELECT * FROM student WHERE Math > 60 AND id > 90;# 查询英语成绩大于语文成绩的同学SELECT *FROM student WHERE English > chinese;# 查询总分大于200分 并且 数学成绩小于等于语文成绩,姓曹的学生.SELECT * FROM student WHERE (English+Math+chinese) > 200 AND Math <= chinese AND `name` LIKE '曹%';
# 函数-- 合计函数-- 统计一个班级共有多少学生SELECT COUNT(*) FROM student;-- 统计数学成绩大于90的学生有多少个SELECT COUNT(*) FROM student WHERE Math > 90;-- 统计总分大于250的人数有多少SELECT COUNT(*) FROM student WHERE (English + Math + chinese) > 250;-- count(*)和count(列)的区别-- count(*) 返回满足条件的记录的行数-- count(列): 统计满足条件的某列有多少个,但是会排除 为null