create database if not exists school default charset=utf8;
use school;
create table if not exists studentinfo(
id int primary key auto_increment,
`name` varchar(20),
sex char,
age int,
address varchar(100)
);
show tables;
insert into studentinfo(`name`,sex,age,address)values("贾宝玉","男",18,"北平"),
("贾政","男",40,"浙江"),("晴雯","女",20,"四川"),("袭人","女",29,"贵州"),
("薛宝钗","女",19,"北平"),("林黛玉","女",17,"浙江");
-- 查看数据 通配符*表示所有
select * from studentinfo;
-- 手动写出字段 全量查询
select id, `name`, sex, age, address from studentinfo;
-- 只查询指定的数据列
select id, `name`, sex from studentinfo;
-- 条件查询
select * from studentinfo where sex='女';
-- 根据查询结果排序 order by
select * from studentinfo order by age desc;
select * from studentinfo order by age desc,id desc;
-- 显示查询结果的条数
select * from studentinfo limit 3 ;
-- 显示查询结果中从索引为5开始的往后3条数据
select * from studentinfo limit 5,3;
-- 复合
select * from studentinfo where sex='女' order by age desc limit 3;
-- 别名AS as 可以省略
select id as 编号, `name` as 姓名, sex as 性别, age as 年龄, address as 地址 from studentinfo;
-- 单条件查询
select * from studentinfo where age<20;
select * from studentinfo where age is not null;
-- 多条件查询
-- 不等于
select * from studentinfo where age!=20;
select * from studentinfo where age<>20;
select * from studentinfo where not (age=20);
-- 与 或
select * from studentinfo where age>20&& sex='女';
select * from studentinfo where age>20 and sex='女';
select * from studentinfo where age>20 || sex='女';
select * from studentinfo where age>20 or sex='女';
-- distinct 去除重复的数据
select distinct `name`, sex, age, address from studentinfo;
数据查询入口
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 获取单个对象上面的例子中filter() 函数返回一个记录集,这个记录集是一个列表。相对列表来说,有些时候我们更需...
- 今天青石的票圈出镜率最高的,莫过于张艺谋的新片终于定档了。 一张满溢着水墨风的海报一次次的出现在票圈里,也就是老谋...