MySQL数据查询
学习数据库的增删改查操作,其中数据库的查询尤为重要,使用一些语句用来查询自己所需要的信息,其中,在面试题上也是尤为常见,因此在此做个查询语句的总结。
语法格式:
select 字段列表|* from 表名
[where 搜索条件]
[group by 分组字段 [having 分组条件]]
[order by 排序字段 排序规则]
[limit 分页参数]
基础查询:
# 查询表中所有列 所有数据
select * from 表名;
# 指定字段列表进行查询
select 字段1,字段2,字段3... from 表名;
Where 条件查询
- 可以在where子句中指定任何条件
- 可以使用 and 或者 or 指定一个或多个条件
- where条件也可以运用在update和delete语句的后面
- where子句类似程序语言中if条件,根据mysql表中的字段值来进行数据的过滤
where条件查询的用法示例:
-- 格式
select * from 表名 where 字段条件;
-- 查询users表中 age > 22的数据
select * from users where age > 22;
-- 查询 users 表中 name=某个条件值 的数据
select * from users where name = '王五';
-- 查询 users 表中 年龄在22到25之间的数据
select * from users where age >= 22 and age <= 25;
select * from users where age between 22 and 25;
-- 查询 users 表中 年龄不在22到25之间的数据
select * from users where age < 22 or age > 25;
select * from users where age not between 22 and 25;
-- 查询 users 表中 年龄在22到25之间的女生信息
select * from users where age >= 22 and age <&#