数据库高级查询1

   tinfo表中有id,name,sex,age字段 

#条件查询 
#查询编号与姓名
select id,name from tinfo;
#给表中字段取别名并查询  可使用as关键字,也可以不用直接在后加别名
select id as 编号,name 姓名 from tinfo;
#查询张三的编号与姓名
select id,name from tinfo where name='张三';
#根据编号查询某条信息
select * from tinfo where id=1;

#模糊查询
#查找姓张的所有信息,用到like可以理解为像 %:任意长度的任意字符 _:任意一个字符  ^:非,不是
select * from tinfo where name like '张%';
#查找姓张与姓李的名字与编号 or:满足一个条件即可
select name,id from tinfo where name like '张_' or name like '李[^三]';
#查询年龄不为空的人的姓名和年龄 is null:空  is not null:非空
select name,age from tinfo where age is not null ;
#查询年龄在18到25之间的人的姓名和年龄 between and:在什么之间,能取到两边
select name,age from tinfo where age between 18 and 25;
select name,age from tinfo where age >=18 and age <= 25;

#去重 distinct ,例:查找张三名字
select distinct name from tinfo where name ='张三';

#按年龄升序排序 order by  默认为升序(asc),降序(desc)
select * from tinfo order by age;
#按多列排序 age为主条件,当age相同时 再将id进行降序排
select * from tinfo order by age,id desc;

#查询前三行的数据 limit 位置偏移量, 行数;
select * from tinfo limit 0,3;
select * from tinfo limit 3;
#查询第三行后的三行数据
select * from tinfo limit 3,3;

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值