首页 > 数据库 >sql查询语句典例整理

sql查询语句典例整理

时间:2022-10-28 15:48:59浏览次数:63  
标签:语句 典例 OrderFee 查询 member 订单 sql WHERE SELECT

简单查询:

1、SELECT * FROM 表名称 WHERE 字段名 LIKE '查询内容'

1)、

SELECT * FROM member WHERE NickName LIKE '贝克汉姆':查询member表NickName字段值为'贝克汉姆'数据

 2)、

SELECT * FROM member WHERE NickName LIKE '贝%':查询member表NickName字段值首位包含“贝”的数据

 3)、

SELECT * FROM member WHERE NickName LIKE ‘%姆':查询member表NickName字段值末位包含“姆”的数据

 4)、

SELECT * FROM member WHERE NickName LIKE '%克%':查询member表NickName字段值中间包含“克”的数据

 

汇总查询:

1、SELECT SUM(字段名) as '总积分' FROM 表名称WHERE 字段名,SUM用于计算总和

1)、

SELECT SUM(Source) as '总积分' FROM member WHERE Source:统计member表Source字段值总和

 2、SELECT COUNT(字段名)  as '总人数' FROM 表名称,COUNT用去统计总数

1)、

SELECT COUNT(NickName)  as '总人数' FROM member :统计member 表NickName字段下的总人数

 2)、

SELECT COUNT( DISTINCT NickName)  as '总人数' FROM member:统计member 表NickName字段下的总人数(去除重复数据)

3)、汇总统计会员注册时间,语句里面的year是按年统计,“CreateTime”是会员注册时间字段,TIMESTAMPDIFF是时间差函数,“now”是基于现在

SELECT id,TIMESTAMPDIFF(year,CreateTime,now()) from member ORDER BY TIMESTAMPDIFF(year,CreateTime,now()) DESC

 

 

3、SELECT MAX(字段名) as '最大值',MIN(字段名) as '最小值' FROM 表名称 ORDER BY 字段名  (ORDER BY是通过排序的方式展示),MAX、MIN用于展示最大最小值

1)、

SELECT MAX(Source) as '最高分',MIN(Source) as '最低分' FROM member ORDER BY Source:查询member表Source字段最大值和最小值

 分组查询:

1、SELECT 字段名 as '积分' ,COUNT(字段名) as '积分人数' FROM 表名称 GROUP BY 字段名( GROUP BY是通过分组的方式展示)

1)、

SELECT Source as '积分' ,COUNT(Source) as '积分人数' FROM member GROUP BY Source:统计各积分拥有相同积分的人数  

 

 带条件查询,

语法基本结构:SELECT  +展示数据(SUM(OrderFee)) +FROM 表(orderinfo ) + WHERE字段(StoreId = 100000000384) +展示方式(GROUP BY StoreId)+条件(HAVING COUNT(OrderNo)>100)或者

SELECT  +展示数据(SUM(OrderFee)) +FROM 表(orderinfo ) + WHERE字段(StoreId = 100000000384) +条件(HAVING COUNT(OrderNo)>100)+展示方式(ORDER BY StoreId)

 1、SELECT 字段名,SUM(字段名) as '全部订单总金额' ,AVG(字段名) as '订单平均值',COUNT(字段名) as '订单总数' FROM 表名称GROUP BY 字段名

1)、

SELECT StoreId ,SUM(OrderFee) as '全部订单总金额' ,AVG(OrderFee) as '订单平均值',COUNT(OrderNo) as '订单总数' FROM orderinfo GROUP BY StoreId:根据店铺ID分组,展示全部订单、订单平均值、订单总数

 2)、

SELECT StoreId ,SUM(OrderFee) as '全部订单总金额' ,AVG(OrderFee) as '订单平均值',COUNT(OrderNo) as '订单总数' FROM orderinfo GROUP BY StoreId  HAVING COUNT(OrderNo)>100:根据店铺ID分组,展示全部订单、订单平均值、订单总数,且保留订单总数大于100的数据

 3)、

SELECT SUM(OrderFee) as '全部订单总金额' ,AVG(OrderFee) as '订单平均值',COUNT(OrderNo) as '订单总数' FROM orderinfo WHERE StoreId = 100000000384:统计出指定店铺下的全部订单总金额、订单平均值、订单总数

 4)、

SELECT StoreId as '店铺ID',COUNT(OrderFee) as '金额人数' FROM orderinfo WHERE OrderFee>50 GROUP BY StoreId :根据店铺ID分组,统计订单金额大于50订单数

5)、

SELECT StoreId,AVG(OrderFee) FROM orderinfo GROUP BY StoreId ORDER BY AVG(OrderFee):根据店铺id分组,计算店铺订单均值排序

 

 6)、

SELECT OrderFee ,OrderNo FROM orderinfo WHERE StoreId = 100000000384  HAVING OrderFee>100 ORDER BY OrderFee DESC:订单金额降序显示(大到小)

SELECT OrderFee ,OrderNo FROM orderinfo WHERE StoreId = 100000000384  HAVING OrderFee>100 ORDER BY OrderFee ASC:订单金额升序显示(小到大)

 

 7)、

SELECT OrderFee,COUNT(OrderFee) FROM orderinfo WHERE StoreId = 100000000384 GROUP BY OrderFee :指定店铺按照订单金额排序且统计每档金额的订单数

8)、区间值查询,统计各区间订单值内的订单数,结果以区间值进行分组显示

select 
    case 
      when OrderFee is null or OrderFee < 100 then '100以内的订单' 
      when OrderFee >= 100 and OrderFee <=1000 then '100到1000的订单'
      when OrderFee > 100000 then '大于10000的订单'
    end 
    as name, 
    count(*) as num
from orderinfo
group by name;

 

 

2、组合条件查询

1)、查询订单价格区间90-100和190-200的订单数据(组合条件查询)

(SELECT *  from orderinfo WHERE Productfee > '90' and Productfee < '100') union
(SELECT *  from orderinfo WHERE Productfee > '190' and Productfee < '200');

 3、跨表查询

1)、统计memberaccount 表里积分数大于100的用户,且(查询)用户在member 表内City值为Changsha(跨表查询)

SELECT MemberID, TotalPoints FROM memberaccount where MemberID  in (SELECT id FROM member WHERE City='Changsha') GROUP BY MemberID HAVING TotalPoints>100

2)、统计memberaccount表TotalFee,member表CreateTime,且以MemberId分组显示。分析查询语句,表后带字段名,FROM一个表JOIN一个表,WHERE接不同字段名但相信数据,这样两个表的数据才能正常匹配

SELECT memberaccount.MemberId , memberaccount.TotalFee, member.CreateTime FROM memberaccount JOIN member WHERE memberaccount.MemberId = member.Id GROUP BY memberaccount.MemberId

 4、跨库查询

1),统计`xkdcore_newfat_ordermanage`库`orderinfo`表内订单数,已MemberId方式分组,且展示订单数大于200的MemberId。统计基于展示的MemberId,查询`xkdcore_newfat_membermanage`库`memberaccount`表下对应MemberId的积分和消费金额

SELECT MemberID, TotalPoints,TotalFee FROM `xkdcore_newfat_membermanage`.`memberaccount` where MemberID  in (SELECT MemberId FROM `xkdcore_newfat_ordermanage`.`orderinfo` GROUP BY MemberID  HAVING COUNT(OrderNo)>200) GROUP BY MemberID 

跨库查询的的关键是,需要在查询语句中写入库名,如“xkdcore_newfat_membermanage”、“xkdcore_newfat_ordermanage”,然后从一个查询结果中提取信息进行二次查询。这里是先查出MemberId,再通过MemberId查询出积分和消费金额

 

 仅执行查询订单数查询语句

SELECT MemberId FROM `xkdcore_newfat_ordermanage`.`orderinfo` GROUP BY MemberID  HAVING COUNT(OrderNo)>200

 

标签:语句,典例,OrderFee,查询,member,订单,sql,WHERE,SELECT
From: https://www.cnblogs.com/becks/p/16738933.html

相关文章

  • Mysql取整函数
    Mysql取整函数1.round四舍五入取整round(s,n):对s四舍五入保留n位小数,n取值可为正、负、零.如四舍五入到整数位,则n取零.2.ceil向上取整ceil(s):返回比s大的最小整数3.floo......
  • MySQL学习
    MySQL学习1.数据库的分类1.1关系型数据库:MySQL、Oracle等通过表与表、行与列的关系进行存储数据。1.2非关系型数据库:Radis等通过存储对象来存储数据,数据由对......
  • mysql09--回表、慢日志、慢查询优化
    1回表#回表查询:先定位主键值,再定位行记录的查询性能比聚集索引(只扫一遍索引树)更低#eg:辅助索引查询对于辅助索引查询方式而言,一共搜索了两棵B+Tree,......
  • like模糊匹配查询慢解决之道——MySQL全文索引
    需求需要模糊匹配查询一个单词select*fromt_phrasewhereLOCATE('昌',phrase)=0;select*fromt_chinese_phrasewhereinstr(phrase,'昌')>0;select*......
  • 修改mysql root密码
    1、如果没有配置环境变量,在\ProgramFiles\MySQL\MySQLServer8.0\bin文件下Shit+右键打开Powershell窗口,如果是从开始菜单选择管理员运行Powershell,则需要cd到上......
  • sqlite3-windows 备份脚本 backup.bat
    sqlite3-windows备份脚本backup.bat 注:其中sqlite3、gzip需要手动下载,放到db目录,下载地址:   sqlite3.exehttps://www.sqlite.org/cli.html   gzip.exeht......
  • MySQL索引以及InnoDB
    二叉树当数据是自增的时候,二叉树会跟链表没有区别平衡二叉树它是一棵空树或它的左右两个子树的高度差的绝对值不超过1,并且左右两个子树都是一棵平衡二叉树。这个方案很......
  • sqllite (2) - c# .net 使用 sqllite 增删改查操作数据库
    sqllite(2)-c#.net使用sqllite增删改查操作数据库环境配置:开发环境:vs2013运行时版本:framework4.0 sqllite版本:非混合模式的静态链......
  • sqllite (1) - c# .net 使用 sqllite 及版本选择问题
    如果一个.NET应用要自适应32位/64位系统,只需要在项目的“目标平台”设置为“AnyCPU”。但是如果应用中使用了SQLite,情况就不同了。SQLite的.NET开发包来自是System.Dat......
  • MySQL生产事故一例
    背景线上日志报错:18:57:54.985[http-nio-8082-exec-9]ERRORo.a.c.c.C.[.[.[.[dispatcherServlet]-Servlet.service()forservlet[dispatcherServlet]incontextwit......