mysql学习日记-1

  1. 基础情况
    help 关键字-显示该关键字的用法
    use 数据库名-选择某个数据库
    show databases-显示数据库列表
    show tables-显示当前数据库的表
    show columns from 表名-显示某个表中的列或者可以用discribe 表名

  2. 检索数据(select1.0)
    select 列名 from 表
    如果列名有多个,加逗号

select a,b,c from table1;

注意:sql语句不区分大小写!
*通配符表示所有列

distinct 去重

select distinct a from table1;

结果:如果原来是
1
1
2
3
现在就是
1
2
3

注意:distinct必须直接放在列名前面,如果选择了多个列,对每个列都使用

limit限制选出的行数

select a from b limit 5
表示限制5行
select a from b limit 1,5
表示从行1开始一共5行(包括行1)
或者用limit 5 offset 1

注意:行从0开始计数

  1. 排序数据(select2.0)
    order by 排序
select a from table1 order by a或者c

select a from table1 order by a,b
先按a如果a中有值相等的,就用看b
select a from table1 order by a desc,b desc limit 5

注意:desc只应用到前面的列名,order在from后面,limit在order后面

  1. 过滤数据(select3.0)
select a from b where a=1或者a='apple'
!= <>都表示不等于

select a from b where a between 1 and 5
a的值在15之间

select a from b where a IS NULL
检查null值

select a,c from b where a=1 and c=2
and可以替换成or(注意and的优先级高于or)

select a from b where a in(1,2)order by a
in与or有近似功能

select a from b where a not in(1,2
  1. 用通配符进行过滤(select4.0)
select a from b where a like 'a%'
%表示匹配0,1,多个字符,但如果末尾有空格,比如某个值为a 这种,不行
_只适配1

注意,适配符要搜索要慢一些

  1. 正则
select a from b where a regexp '.000'
order by a
.匹配1个,只要a的值包含x000,就会被选出来
select a from b where a regexp binary 'Apple .000'
order by a
区分大小写
select a from b where a regexp '1000|2000'
order by a
1000或者2000
select a from b where a regexp '[123] apple'
order by a
123
select a from b where a regexp '[1|2|3] apple'
order by a
一样的,不可以去掉括号,去掉的话默认3和apple一起了
要否定,[123]
\\双斜杠转义
select a from b where a regexp '^[123] apple'
order by a
只匹配开头
$匹配后面

* +{}

注意regexp是包含就可以 like是全匹配

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值