首页 > 数据库 >mysql的学习

mysql的学习

时间:2024-07-19 12:27:42浏览次数:10  
标签:cus sum mysql 学习 sql total where payment

p29  cross join(笛卡尔积)有显示与隐式
p30   union 联合起来的列数要相同,且列名取决于第一个select

SELECT  cus.customer_id,
cus.first_name,
cus.points,
'Bronze' AS type
FROM sql_store.customers cus
where cus.points<1000
union
SELECT  cus.customer_id,
cus.first_name,
cus.points,
'Sliver' AS type
FROM sql_store.customers cus
##where cus.points<2000 and cus.points>=1000
where points between 1000 and 2000
union
SELECT  cus.customer_id,
cus.first_name,
cus.points,
'Gold' AS type
FROM sql_store.customers cus
where cus.points>=2000
order by first_name

使用where查询时,小的值放前面,列如:between 1000 and 2000

获取最新插入的数据的id   LAST_INSERT_ID()

create table 'name' AS
select * from 'table'
USE sql_invoicing;
##create table invoices_archived AS
insert into invoices_archived
select *
from invoices ies
left join clients cs
using (client_id)
where ies.payment_date is not null;


insert into 'tablename'
select * from 'table'

update single row 
update multiple row

update set

USE sql_store;
update customers
set points=points+50
where birth_date<'1990-01-01'

在更新中使用子查询
USE sql_store;
update orders
set comments='Gold'
where customer_id in
(
select customer_id
from customers
where customers.points>3000
)
恢复数据库
file->open sql script


//第五章聚合函数
aggregate Function must USE ()进行调用或者执行
distinct  表示唯一

例题使用到了 union
SELECT 
        'First half of 2019' AS date_range,
        sum(invoice_total) AS total_sales,
        sum(payment_total) AS total_payments,
        sum(invoice_total-payment_total) AS what_we_expect
FROM sql_invoicing.invoices
where invoice_date 
between '2019-01-01' and '2019-06-30'
union
SELECT 
        'Second half of 2019' AS date_range,
        sum(invoice_total) AS total_sales,
        sum(payment_total) AS total_payments,
        sum(invoice_total-payment_total) AS what_we_expect
FROM sql_invoicing.invoices
where invoice_date between '2019-07-01' and '2019-12-31'
union
SELECT 
        'Tataol' AS date_range,
        sum(invoice_total) AS total_sales,
        sum(payment_total) AS total_payments,
        sum(invoice_total)-sum(payment_total) AS what_we_expect
FROM sql_invoicing.invoices
where invoice_date between '2019-01-01' and '2019-12-31'
;

sum() 函数两种写法都可以


GROUP BY 子句
先分组再排序
SELECT 
    pay.date,
    paym.name,
    sum(amount) AS total_payments
FROM sql_invoicing.payments pay
join payment_methods paym
on pay.payment_method=paym.payment_method_id
group by pay.date,paym.name
order by date;

having 子句(是group by 后面的条件查询====数据筛选)
where 可以使用原表中的列,但是having只能使用select使用到的列,因为having 是服务group by的

SELECT 
cs.customer_id ,
cs.first_name,
cs.last_name,
sum(oits.quantity*oits.unit_price) as totalPay
FROM sql_store.customers cs
join orders os
using (customer_id)
join order_items oits
using (order_id)
where state='VA'
group by cs.customer_id ,
cs.first_name,
cs.last_name
having totalPay>100

with rollup(应用于聚合列----对每一组的分组做统计  使用时 不能用别名)
SELECT 
pay.name as payment_method,
sum(amount) as total
FROM sql_invoicing.payments  invpay
join payment_methods pay
on  invpay.payment_method=pay.payment_method_id
group by pay.name with rollup
order by total


编写复杂查询
subqueries

SELECT *
FROM sql_hr.employees
where salary>(
                select avg(salary)
                from sql_hr.employees
                );
                
IN Operator

标签:cus,sum,mysql,学习,sql,total,where,payment
From: https://blog.csdn.net/qq_47307647/article/details/140544272

相关文章

  • 初步学习JDNI注入
    最近学习java安全,JDNI注入必需了解,进行初步的学习首先JNDI注入对JAVA版本是有限限制的,本地是1.8.0_2.0.1超过了191,所以最开始拿符合版本方法复现没有成功java版本查看JNDI(JavaNamingandDirectoryInterface)是一个应用程序设计的API,一种标准的Java命名系统接口。JNDI......
  • Vue3学习(未完待续)
    Vue3vite全新的前段开发工具就是webpack的代替品npminitvite-appvue3testnpmi模板中可以没有根标签安装vue3的测试工具常用CompositionAPIsetup是vue3中的一个配置项<script>import{h}from'vue'//返回一个渲染函数exportdefault{ name:"app", setup(){......
  • nginx学习记录
    目录监听端口nginx缓冲和缓存缓冲优点与作用缓存优点与作用nginx负载均衡权重健康检查监听端口假设代码中,我监听8003端口,意味着我可以通过访问8003端口来获得数据将所有请求进行转发,即访问8003端口,nginx会将“访问8003端口”这一请求,转发到设定的地址这就实现了,访问的是8003......
  • MySQL中的using关键字
    先创建两张表CREATETABLEemployees(employee_idINTAUTO_INCREMENTPRIMARYKEY,nameVARCHAR(50),positionVARCHAR(50),salaryDECIMAL(10,2));INSERTINTOemployees(employee_id,name,position,salary)VALUES(1,'JohnDoe'......
  • 机器学习中常用的数据类型
    常用的数据类型有FP64、FP32、FP16、BFLOAT16等FP64FP64表示64位浮点数,通常为IEEE754定义的双精度二进制浮点格式,由1位符号位、11位指数位、52位小数位组成表示范围:正数范围:约4.9x10e-324~1.8x10e308负数范围:约-1.8x10e308~-4.9x10e-324通常用于精度要求......
  • RabbitMQ学习实践一:MQ的安装
    文章是本人在学习springboot实现消息队列功能时所经历的过程的记录,仅供参考,如有侵权请随时指出。参考文章地址:RabbitMQ安装与入门_rabbitmqwin11配置-CSDN博客RabbitMQ入门到实战一篇文章就够了-CSDN博客RabbitMQ系列(18)--RabbitMQ基于插件实现延迟队列_rabbitmq延迟队列插......
  • 【CSS学习第一篇】
    CSS学习第一篇1.CSS简介1.1什么是CSS?1.2CSS语法规范2.CSS选择器2.1CSS选择器的作用2.2CSS选择器的分类2.3标签选择器2.4类选择器2.5id选择器2.6通配符选择器3.CSS字体属性3.1font-family设置字体系列3.2font-size字号大小3.3font-weight字体粗细3.4font......
  • 暑假两个月学习AI产品经理详细路线,看这一篇就够了
    以下是一个暑假期间学习AI产品经理的详细路线,分为八个周来进行:第1周:了解AI产品管理基础阅读材料:《人工智能:一种现代的方法》了解AI基础。《人人都是产品经理》了解产品管理基础。在线课程:Coursera上的“人工智能基础”课程。edX上的“产品管理基础”课程。实践:调研......
  • Nodify学习 三:连接器
    前置连接概述连接是由两个点之间创建的。Source和Target依赖属性是Point类型,通常绑定到连接器的Anchor点。基本连接库中所有连接的基类是BaseConnection,它派生自Shape。在创建自定义连接时,可以不受任何限值地从BaseConnection派生。它公开了两个命令及其对应的事件:Disconne......
  • 计算机毕业设计Python+Tensorflow小说推荐系统 K-means聚类推荐算法 深度学习 Kears
    2、基于物品协同过滤推荐算法2.1、基于⽤户的协同过滤算法(UserCF)该算法利⽤⽤户之间的相似性来推荐⽤户感兴趣的信息,个⼈通过合作的机制给予信息相当程度的回应(如评分)并记录下来以达到过滤的⽬的进⽽帮助别⼈筛选信息,回应不⼀定局限于特别感兴趣的,特别不感兴趣信息的纪录也相......