目录
查询入学日期到'1998-09-01' 到 '1999-09-01' 的学员信息
查询年龄等于18岁或者年龄等于20岁或者年龄等于22岁的学员信息
条件查询语法
SELECT 字段列表 FROM 表名 WHERE 条件列表 ;
条件
创建stu表
-- 创建stu表
CREATE TABLE stu (
id INT,-- 编号
NAME VARCHAR ( 20 ),-- 姓名
age INT,-- 年龄
sex VARCHAR ( 5 ),-- 性别
address VARCHAR ( 100 ),-- 地址
math DOUBLE ( 5, 2 ),-- 数学成绩
english DOUBLE ( 5, 2 ),-- 英语成绩
hire date -- 入学时间
);-- 添加数据
INSERT INTO stu ( id, NAME, age, sex, address, math, english, hire )
VALUES
( 1, '马晕晕', 55, '男', '杭州', 66, 78, '1995-09-01' ),
( 2, '马画藤', 45, '女', '深圳', 98, 87, '1998-09-01' ),
( 3, '马斯克', 55, '男', '香港', 56, 77, '1999-09-02' ),
( 4, '柳萨法', 20, '女', '湖南', 76, 65, '1997-09-05' ),
( 5, '阿斯顿', 20, '男', '湖南', 86, 99, '1998-09-01' ),
( 6, '刘德花', 57, '男', '香港', 99, 99, '1998-09-01' ),
( 7, '张学右', 22, '女', '香港', 99, 99, '1998-09-01' ),
( 8, '阿尔法', 18, '男', '南京', 56, 65, '1994-09-02' );
查询列表
SELECT * FROM stu;
查询地址信息
select address from stu;
去除重复记录 关键字:DISTINCT
select DISTINCT address from stu;
查询姓名,数学,英语成绩
select name , math , english from stu;
注意:代码变化以及结果
select name , math as 数学成绩 , english as 英语成绩 from stu;
条件查询
查询年龄大于20岁的学员信息
select * from stu where age > 20;
查询年龄大于的等于20岁的学员信息
select * from stu where age >= 20;
查询年龄大于的等于20岁并且小于等于30岁的学员信息
-- 三种方法均行
select * from stu where age >= 20 && age <= 30;
select * from stu where age >= 20 and age <= 30;
select * from stu where age BETWEEN 20 and 30;
查询入学日期到'1998-09-01' 到 '1999-09-01' 的学员信息
select * from stu where hire BETWEEN '1998-09-01' and '1999-09-01';
查询年龄等于18岁的学员信息
select * from stu where age = 18;
查询年龄不等于18岁的学员信息
-- 两种方法均行
select * from stu where age != 18;
select * from stu where age <> 18;
查询年龄等于18岁或者年龄等于20岁或者年龄等于22岁的学员信息
-- 两种方法均行
select * from stu where age =20 or age=18 or age=22;
select * from stu where age in (18,20,22);
特别注意:null值比较不能使用= !=. 需要使用is is not。
通配符
- _:代表单个任意字符
- %:代表任意个数字符
-- 1.查询姓'马'的学员信息
select * from stu where name like '马%';
-- 2.查询第二个字是'花'的学员信息
select * from stu where name like '_晕%';
-- 3.查询名字中包含'德'的学员信息
select * from stu where name like '%德%';