数据库基本知识一

1检索数据

1).单列
select name –检索列的名字
from new_table; –检索来自哪个表
2)多列
select name**,**id –检索列的名字
from new_table; –检索来自哪个表
3)所有列
select * –检索列的名字
from new_table; –检索来自哪个表
4)不同的值
select distinct name –检索列的名字
from new_table; –检索来自哪个表
5)限制结果
select name –检索列的名字
from new_table –检索来自哪个表
limit 2;–输出前两列

2排序检索数据

1)按多个列排序
select name**,id ,**price –检索列的名字
from new_table –检索来自哪个表
order by price,id; (3,2) –第二列为price,第三列为id
2)指定排序方向
(1)降序
select name**,id ,**price –检索列的名字
from new_table –检索来自哪个表
order by price desc,id; (3,2) –第二列为price 降序排列,第三列为id
(2)升序
select name**,id ,**price –检索列的名字
from new_table –检索来自哪个表
order by price asc,id; (3,2) –第二列为price 降序排列,第三列为id

3过滤数据

1).单个值
select name,id –检索列的名字
from new_table –检索来自哪个表
**where**id<10;–输出id小于10的
2)不匹配
select name,id –检索列的名字
from new_table –检索来自哪个表
where name <> (!=) ‘张三’;–输出name不是张三的,数字可以不用加单引号
3)范围值
select name,id –检索列的名字
from new_table –检索来自哪个表
where id between 5 and 10;–输出id5到10之间的值
4)空值检查
select name,id –检索列的名字
from new_table –检索来自哪个表
where id is null;–输出id不存在的值
5)and或者or
select name,id –检索列的名字
from new_table –检索来自哪个表
where id <10 and(or) name !=’张三’;–输出id小于10并且(或者)名字不是张三的or
注:order by 语句应该放在where语句后面
or和and同时存在时,若先判断or则加括号区分
6)in操作符
select name,id –检索列的名字
from new_table –检索来自哪个表
where id in(5,10);–输出id5或10。与or功能相同,文字加单引号
6)not操作符
select name,id –检索列的名字
from new_table –检索来自哪个表
where not id=5;–输出id不等于5的值

4用通配符匹配

1)百分号
select name,id –检索列的名字
from new_table –检索来自哪个表
where name like ‘%y’;–输出y结尾的,不管前面有多少字符
2)下划线
select name,id –检索列的名字
from new_table –检索来自哪个表
where name like ‘_y’;–输出y结尾的,只匹配前面一个字符
3)方括号
select name,id –检索列的名字
from new_table –检索来自哪个表
where name like ‘[JK]%’;–输出J或K开头的。[JK]匹配括号中任意一个字符,若否定Wie[^JK]

5创建计算字段

1)拼接字段
select concat(name,’(‘,id,’)’ –name(id)
from new_table; –检索来自哪个表
2)去掉空格
select rtrim(name),’(‘,rtrim(id),’)’ –name(id)
from new_table; –检索来自哪个表
3)使用别名as
select name,id,age,id*age as new_id –创造新列
from new_table;–检索来自哪个表

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值