其他分享
首页 > 其他分享> > 数据的获取

数据的获取

作者:互联网

*数据源:储存在demo数据库的chapter5表中
在这里插入图片描述

一、获取列:

1.获取全部列:

select * from demo.chapter5

2.获取特定列:

select
       id , class
from 
      demo.chapter5

(获取的是id和class两列)

二、获取行:

1.获取全部行:

select * from demo.chapter5

2.获取前几行:

select
       * 
from 
      demo.chapter5
 limit 5

(获取的前五行的数据)
3.获取第x行以后的y行:

select
       * 
from 
      demo.chapter5
 limit 2,3

4.获取满足单一条件的行:

select
       * 
from 
      demo.chapter5
 where age = 18

(获取age列等于18的行)
5.获取满足多个条件的行:

select
       * 
from 
      demo.chapter5
 where age = 18 and class = "一班"

(获取age列等于18且class列等于一班的行)

select
       * 
from 
      demo.chapter5
 where age = 18 or class = "一班"

(获取age列等于18或class列等于一班的行)

三、同时获取行和列:

select
       id 
       ,class
from 
      demo.chapter5
 where age = 18 and class = "一班"

(获取age列等于18且class列等于一班的id和name列)

四、插入一列固定值:

select
       id
       ,name
       ,"age<20" as label
from 
      demo.chapter5
 where age < 20

(插入label列用来表示age列小于20的id和name列)

五、JSON列解析:

*JSON格式:一种数据结构——{key:value}的形式,key:字段,value:取值

select
       id
       ,json_extract(score,'$.数学') as "数学成绩"
from 
      demo.chapter5

(插入数学成绩列,用来表示获取score列每个id对应的value)

select
       id
       ,json_keys(score) as "科目"
from 
      demo.chapter5

(插入科目列,用来表示获取score列每个id对应的key)

六、对结果进行排序:

select
      *
from 
      demo.chapter5
order by age

(按照age列进行升序排列)

select
      *
from 
      demo.chapter5
order by age desc

(按照age列进行降序排列)

select
      *
from 
      demo.chapter5
order by class asc
        , age desc

(按照class列升序,age列降序排列)

标签:chapter5,demo,age,获取,数据,class,select
来源: https://blog.csdn.net/weixin_44790641/article/details/115794927