MySql必知必会 笔记chapter3-10

3

use crashcourse; #使用crashcourese数据库
show databases; #列出所有数据库
show tables; # 列出数据库内的表的列表
show columns from customers; #从表customers中对每个字段返回一行
describe customers; #与上一句等价

自动增量:某些列需要唯一值,mysql可以自动为每个行分配下一个可用编号

show status; #显示广泛的服务器状态信息
show create database db_name; #显示创建特定数据库
show create table tbl_name; #显示创建特定数据表
show grants; #显示授予用户的安全权限
show errors;
show warnings;
help show;

4 检索数据 select from

select prod_name from products;#从products表中检索prod_name的列,未排序
select prod_id, prod_name, prod_price from products;  #检索多个列
select *  from products; #检索所有列
select distinct vend_id from products; #只返回不同的vend_id
select prod_name from products limit 5; #limit 5 返回不多于5行
select prod_name from products limit 5,5 ; #从行5开始的5行,其实是第6行开始
#如果行数不够则只返回有的行,limit 4 offset 3意味从行3开始取4行等价于limit 3,4
select products.prod_name from crashcourse.producs; #用点号来限定

5排序检索数据 order by desc asc

select prod_name from products order by prod_name; #order by 也可使用非选择的列排序
select prod_id, prod_price, prod_name from products order by prod_price, prod_name;    #先按照价格,再按名字排序
select prod_name from products order by prod_name desc ;  #降序排列
select prod_id, prod_price, prod_name from products order by prod_price desc , prod_name; #prod_price 降序, prod_name还是升序默认
select prod_price from products order by prod_price desc limit 1; #找最值

6过滤数据 where = , <> , != , between and ,NULL
同时使用order by 和 where ,应该order by放在where后面

select prod_name, prod_price from products where prod_price=2.50;
select prod_name , prod_price from products where prod_name='fuses';
select prod_name, prod_price from products where prod_price<=10;
select prod_name, prod_price from products where prod_price between 5 and 10;
select prod_name from products where prod_price is NULL; #检索空值

7数据过滤 and or in not
先and 再计算or

select prod_id, prod_price, prod_name from products where vend_id=1003 and prod_price<=10; #and 连接两个条件
select prod_id, prod_price, prod_name from products where vend_id=1002 or vend_id=1003 and prod_price>=10; #计算顺序优先处理and,应该
#使用括号来明确计算顺序
select prod_name, prod_price from products where vend_id in (1002, 1003) order by prod_name; #与or等价
select prod_name, prod_price from products where vend_id not in (1002, 1003) order by prod_name; #not 否定条件关键字

8通配符进行过滤 like
%可以匹配0个1个或多个字符,不能匹配null
_只匹配单个字符

select prod_id, prod_name from products where prod_name like 'jet%'; #其中%表示任何字符出现任意次数
select prod_id, prod_name from products where prod_name like '%anvil%'; #在任何位置使用%
select prod_id, prod_name from products where prod_name like '_ ton anvil'; # 

通配符搜索处理一般比较慢不要过度使用

9正则表达式进行搜索 regexp
如where prod_name regexp binary 'JetPack .000’可以区分大小写

select prod_name from products where prod_name regexp '1000' order by prod_name; #匹配‘1000’
select prod_name form products where prod_name regexp '.000' order by prod_name; #其中. 匹配任意一个字符
select prod_name from products where prod_name regexp '1000 | 2000' order by prod_name; # 使用|为正则表达式的or操作
select prod_name from products where prod_name regexp '[123] Ton' order by prod_name; #[123]定义一组字符,匹配1或2或3
#如果是regexp '1|2|3 Ton' 则会认为匹配 1或2或3 Ton,[^123]匹配除了123以外的字符

[0-9]用-来定义一个范围,用\为前导匹配特殊字符,\f换页,\n换行,\r回车,\t制表,\v纵向制表
[:cntrl:]ASCII控制字符(ASCII0到31和127)
‘* + ? {n} {n,} {n,m}’

select prod_name from products where prod_name regexp '\\([0-9] sticks?\\)' order by prod_name; 
#(1 stick) 以及(5 sticks) ?表示有0或者1个s字符
select prod_name from products where prod_name regexp '^[0-9\\.]' order by prod_name;
#^匹配串的开始,注意如果在[]中则表示否定,以数字或者.为开头的字符串

定位元字符 ^文本的开始,$文本的结尾,[[:<:]]词的开始, [[:>:]]词的结尾
like匹配整个字符串而regexp匹配子串

10创建计算字段
多数dbms使用+或||而mysl使用concat()实现拼接

select concat(vend_name, ' (', vend_country, ' )'  as vend_title from vendor order by vend_name;
#concat拼接了4个元素,as使用了个别名,对于客户机引用时就像真实列表一样。
select prod_id, quantity, item_price , quantity*item_price as expanded_price from orderitems where order_num=20005;
#输出显示的expend_price列为一个计算字段
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值