数据库查询

 

                 base_user 人物表                                                 base_role 角色表                    

--in

select *from 表名 where 列名 in ()

例如:select from  base_user  where t_status in ('1','2')

查询表中t_status等于'1'或者等于'2'的数据

--not in

select *from 表名 where 列名 not in ()

例:select * from base_user where t_city not in ('成都','上海')

查询表中不为成都或者上海的数据

--is

select *from 表名  where  列名 is 

select * from  base_user  where    t_money is null

查询金额是空的数据

--is  not

select *from 表名  where  列名 is not

select * from  base_user  where    t_money is not  null

查询金额不是空的数据

--like 表示匹配 %代表匹配所有

select *from 表名 where 列名 like 

 select *from  base_user where t_city  like '郑%'

查询所有以郑开头的数据

  '%州%' 包含州的所有数据

--not  ike 表示匹配 %代表匹配所有

select *from 表名 where 列名 not like 

 select *from  base_user where t_city not  like '%州%'

查询所有不包含州的数据

查询结果当做条件去使用

select *from base_user where role_id in (select id from base_role where t_code='4')

查询人物表中role_id与角色表中t_code等于4的所有id数据相同的所有数据

查询结果当成是一个要被查询的字段去使用

select *,(select t_name from base_role where id=a.role_id) as 角色名 from base_user as a

查询任务表中所有人的角色名称

查询结果作为一个虚拟的表去使用

select *from (select id,t_name,t_city,role_id from base_user) as a

查询人物表中查询这几个字段构成的虚拟表的所有数据

括号内的查询可以进行分组,但不能进行排序

分组

select  列名,列名   from  表名   group by  列名,列名

select t_city   from  base_user group by  t_city

查询以城市进行分组的所有城市数据.  查询出的字段必须与分组字段一致,以什么分组就查询什么字段,聚合函数除外

排序 order by 默认正序 asc 倒序desc  排序有先后顺序

select * from  表名  order by 列名,列名

select * from  base_user  order by role_id

以角色id进行排序

先条件在分组后排序

select t_city,role_id base_user

where role_id>3

group by t_city,role_id

order by role_id desc

--数据库基础函数

--1,查询字符出现的位置,下标从1开始
select charindex('de','abcdefg')  

--2,查询字符串的长度
 select len('abcdefg') 

--3,去掉空格

select ltrim('     a    ')     --去掉左边空格

select rtrim('       a  ')    --去掉右边空格

--4,截取字符串

select left ('abcdefg', 3)  --从左边数截取三个

select right('abcdefg',3)  --从右边数截取三个

select substring('abcdefg',2,3) --从下标为2的字符开始截取三个

--5,取整

select floor (22.55)  --向下取整

select ceiling(22.55) --向上取整

--6,转大小写

select upper('abcdefg') --转大写

select lower ('ABCDEFG') --转小写

--7,取绝对值

select abs(-11)

--8,四舍五入

select round(23.56,1) --保留一位小数

--9,求次幂

select power(4,3) --查询4的3次幂

--10,获取当前的时间

select getdate();

聚合函数

select 

t_city,

count (id) as num,       --获取含有id的总条数

min (t_money)  as minmoney --金额的最小值

max(t_money)  as maxmoney  --金额的最大值

avg(t_money)  as  avgmoney   --金额的平均值

sum(t_money) as  summoney   --金额的总和

from 

  base_user

group by

    t_city

关联查询

1,左关联查询(以left join左边数据表为主)

SELECT
    a.id AS useID,
    a.t_name AS usename,
    a.t_city AS usecity,
    a.t_money AS usemoney,
    a.t_is_admin AS admin,
    a.t_status AS status,
    a.role_id AS roleid ,
    b.t_name  as rolename,
    b.t_code as  rolecode
FROM
    base_user AS a
   LEFT JOIN base_role AS b ON b.id = a.role_id

2,右关联查询

SELECT
    a.id AS useID,
    a.t_name AS usename,
    a.t_city AS usecity,
    a.t_money AS usemoney,
    a.t_is_admin AS admin,
    a.t_status AS status,
    a.role_id AS roleid ,
    b.t_name  as rolename,
    b.t_code as  rolecode
FROM
    base_user AS a
   right JOIN base_role AS b ON b.id = a.role_id

内连接查询(只有两边的数据表都有数据的情况下才会显示)

SELECT
    a.id AS useID,
    a.t_name AS usename,
    a.t_city AS usecity,
    a.t_money AS usemoney,
    a.t_is_admin AS admin,
    a.t_status AS status,
    a.role_id AS roleid ,
    b.t_name  as rolename,
    b.t_code as  rolecode
FROM
    base_user AS a
  INNER JOIN base_role AS b ON b.id = a.role_id

外连接查询(两边数据表任意一个有数据,都会显示)

SELECT
    a.id AS useID,
    a.t_name AS usename,
    a.t_city AS usecity,
    a.t_money AS usemoney,
    a.t_is_admin AS admin,
    a.t_status AS status,
    a.role_id AS roleid ,
    b.t_name  as rolename,
    b.t_code as  rolecode
FROM
    base_user AS a
   FULL JOIN base_role AS b ON b.id = a.role_id

合并两张表的数据,注意上下两张表的字段数量要一致,字段数据顺序要一致,,字段数据完全一致时会进行去重操作

select t_name from base_user

union

select t_name from base_role

--不去重  合并两张表的数据

select t_name from base_user

union  all

select  t_name from  base_role

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

玉玊则不达

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值