数据库常用结构化sql语句_2

六、查询数据

  查询数据的sql是我们使用频率最高的sql,而这些SQL的优化程度代表着一个人对数据库的熟悉程度,所以我们在查询部分会涉及到很多的知识点。

1) 查询所有与查询个别字段

1

 select username,password  form  user;

 2)对部分列起别名

1

2

select  username as  loginname  form  user;

select  username  loginame,password  from user; # 注意将新的列名和旧列名空格隔开

 3)去掉重复值 distinct

1

2

select  distinct  username from  user;

select   distinct  username  loginame ,password  from user;

 4)使用where条件查询

1

select from user  where  **** ;

 5)对空值的查询
  虽然我们在创建表结构的时候一般设置 not null 但是也有些表会出现null的情况,例如我们将user表更改一下,表里面的数据如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

1    kailinux    javadocker

2    LInuxmysql    NULL    NULL为字符NULL

3    python    NULL

4    LInux    redis

5    linux    0

6    redis    name 

7    kailinux    javadocker

8    LInux    redis

9    kailinux         #值为' '

10    redis    name 

15    python    javadocker

16    LInux        #值为' '

17    linux    999

18    redis    name 

19    ubuntu       #值为默认的NULL

20    centos      #值为默认的NULL

  经分析得,表里面不仅有NULL值还有‘ ’值,我们现在研究一下为NULL和为' '的查询,对于NULL一般不用,整形默认为0,字符串模式为" "

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

select from user where password=' ';

结果

id   username      password

9    kailinux   

16    LInux  

#查找password列为NULL的方法

select from user where password=NULL 

结果为空

select from user where password='NULL' 

结果为:

2    LInuxmysql    NULL

3    python    NULL

select *  from  user where password  is NULL;

结果为:

19    ubuntu    

20    centos    

**所以当我们要查询默认为NULL的时,需要用is NULL查询

6)条件判断 in和 between...and...

  where 条件判断

7)like 模糊查询

like中 %p匹配任意多个字符  _  下划线匹配一个字符

1

2

3

4

5

6

7

8

9

10

select from user where username like '%linux%'  #查询用户中包含linux的用户,不区分大小写

1    kailinux    javadocker

2    LInuxmysql    NULL

4    LInux    redis

5    linux    0

7    kailinux    javadocker

8    LInux    redis

9    kailinux    

16    LInux    

17    linux    999

8)使用正则表达式

  mysql是非常牛的一个数据库,不仅函数多,而且支持正则表达式,接下来我们研究一下正则表达式的匹配。 regexp  效率会比like差一点

regexp 正则里面

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

select from user where username regexp '^linux'  #以linux开头的

结果:不区分大小写

2    LInuxmysql    NULL

4    LInux    redis

5    linux    0

8    LInux    redis

16    LInux    

17    linux    999

select from  user  where   username regexp 'python|redis' ; #用户名中包含redis和python的

3    python    NULL

6    redis    name 

10    redis    name 

15    python    javadocker

18    redis    name 

21    PYTHonmysql    

22    rediswinner    

23    PYthonmysql    

24    winnerredis    

select from  user  where   username regexp ' python$|redis$|234$' ; 查询以python,redis 

234结尾的用户名

6    redis    name 

10    redis    name 

18    redis    name 

24    winnerredis    

25    PYTHoREDIS    

27    PYthon234    

28    winner1234

9 对查询结果排序  order by   desc  asc

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

select  *   from user order by id; #默认升序

1    kailinux    javadocker

2    LInuxmysql    NULL

3    python    NULL

4    LInux    redis

5    linux    0

 .......   #部分结果省略

23    PYthonmysql    

24    winnerredis    

25    PYTHoREDIS    

26    redispyhon    

27    PYthon234    

28    winner1234   

 select  *   from user order by id desc;  #降序排列 desc降序

28    winner1234

27    PYthon234

26    redispyhon

25    PYTHoREDIS

.......    #部分结果省略

8    LInux

7    kailinux

6    redis

5    linux

4    LInux

3    python

2    LInuxmysql

1    kailinux

select  *   from user order by id asc;  

结果:

1    kailinux    javadocker

2    LInuxmysql    NULL

3    python    NULL

4    LInux    redis

5    linux    0

 .......   #部分结果省略

23    PYthonmysql    

24    winnerredis    

25    PYTHoREDIS    

26    redispyhon    

27    PYthon234    

28    winner1234  

 默认排序方式为asc方式

     常用的结构化查询语上上篇内容基本结束,在下篇中会从续查询部分内容,完成我们常使用的结构化查询语句的介绍,由于本人水平有限,内容缺乏精彩片段,但是愿意和各位朋友一起分享学习,请各位朋友多指导!

猜你喜欢

转载自blog.csdn.net/choupiaoyi2794/article/details/81185668