mysql学习笔记(5)

来源:互联网 发布:网络提现 需要牌照 编辑:程序博客网 时间:2024/05/18 22:44

查询——select


1.查询【user】表中数据 

select * from user;    # 查询【user】表中所有数据

select id,name from user;    #查询【user】表中id列和name列的数据


2.给字段取个别名   as/空格

select id,name( as) n,pass( as) p from user;


3.distinct关键字的使用(取出唯一值)

select distinct age from user  #  把age列的值取出并去重)


4.使用where条件进行查询

select * from user where id=3;


5.查询空值null和非空值

select * from user where pass is null;

select * from user where pass is not null;


6.between and 的用法

select * from user where id between 3 and 5;


7.in 的用法

select * from user where id in(1,3,5);


8.like 的使用方法(模糊查询)

# %匹配所有字符,_匹配一个字符

select * from user where name like '%4%';

select * from user where name like '%4%' or name like '%5%';


# 用正则表达式(regular express)# 但是会慢

select * from user where name regexp '4';

select * from user where name '(.*4.*)|(.*5.*)';

# 正则复习

'4'   # 包含4的   

'4$'    # 以4结尾的

’^u'    # 以u开头的

'.*s.*'    # 包含s的


9.使用order by 对结果进行排序

select * from user order by name;

# 默认升序排列,升序"... order by ... asc;",降序"...order by ... desc;"

# 按字段排序,如果是string并不是按自然数排序,string需要按数字大小排序方法如下

select * from user order by -name asc;   # 降序

select * from user order by -name desc;  # 升序

select * from user order by (name+1);  # 升序


10.使用limit限定输出个数

select * from user order by name limit 1,3;

limit 1,3  # 从1号位(第二位)开始取3个

limit 5   #从开头取5个




0 0
原创粉丝点击