粉粉蕉的笔记本粉粉蕉的笔记本
  • JAVA

    • 代码笔记
    • Java8实战
    • 分布式事务实战(Seata)
    • 模板引擎(FreeMarker)
    • SpringSecurity
  • PYTHON

    • 概述
    • python3
    • python3(菜鸟教程)
    • pandas
    • numpy
    • matplotlib
  • 中间件

    • Kafka
    • RocketMQ
    • Redis
    • MongoDB
    • Elastic Search
  • 数据库

    • Mysql
  • 设计模式
  • 运维

    • linux命令速查
    • windows命令速查
    • Docker笔记
    • kubernetes学习笔记
    • kubernetes实操笔记
    • 运维工具大全
    • git操作宝典
  • 大数据

    • 概览
    • Hadoop
    • Hive
  • 机器学习

    • 机器学习概览
  • 概率论
  • 线性代数
  • 统计学
  • 金融知识学习
  • 聚宽
  • 因子分析
  • RSS
  • 资源导航
  • 医保
  • 健身

    • 笔记
    • 训练计划
  • 装修攻略
  • 读书笔记

    • 《深度学习》
我也想搭建这样的博客!
🚋开往
  • JAVA

    • 代码笔记
    • Java8实战
    • 分布式事务实战(Seata)
    • 模板引擎(FreeMarker)
    • SpringSecurity
  • PYTHON

    • 概述
    • python3
    • python3(菜鸟教程)
    • pandas
    • numpy
    • matplotlib
  • 中间件

    • Kafka
    • RocketMQ
    • Redis
    • MongoDB
    • Elastic Search
  • 数据库

    • Mysql
  • 设计模式
  • 运维

    • linux命令速查
    • windows命令速查
    • Docker笔记
    • kubernetes学习笔记
    • kubernetes实操笔记
    • 运维工具大全
    • git操作宝典
  • 大数据

    • 概览
    • Hadoop
    • Hive
  • 机器学习

    • 机器学习概览
  • 概率论
  • 线性代数
  • 统计学
  • 金融知识学习
  • 聚宽
  • 因子分析
  • RSS
  • 资源导航
  • 医保
  • 健身

    • 笔记
    • 训练计划
  • 装修攻略
  • 读书笔记

    • 《深度学习》
我也想搭建这样的博客!
🚋开往
    • 概述
    • SQL速查

SQL速查

列转行

select 
teacher_name,
GROUP_CONCAT(student_name) students
from student
group by teacher_name

student表:

teacher_namestudent_name
黄老师小明
黄老师小红
红老师小黑

结果:

teacher_namestudents
黄老师小明,小红
红老师小黑

获取子字符串

-- 获取前2个字符(黄老师 -> 黄老)
select SUBSTRING(student_name,1,2) 
from student;

-- 获取第2个字符开始,后面的子字符串(黄老师 -> 老师)
select SUBSTRING(student_name,2) 
from student;

-- 获取倒数2个字符串(我是黄老师 -> 老师)
select SUBSTRING(student_name,-2) 
from student;

字符串拼接

-- 用CONCAT函数。支持多个入参
select
CONCAT('A','B',student_name,'HAHAHA')
from student;

DISTINCT 的用法

加在SELECT之后,比如:

select distinct name,age from person;

用于对查出来的行进行去重。

也可以用于group by的语句:

select student_name,count(distinct subject) 
from course group by student_name;

多字段分组,并取最新那条

SELECT t1.*
FROM tableName t1
INNER JOIN (
  SELECT groupId, MAX(created_at) AS maxDate
  FROM tableName
  GROUP BY groupId
) t2 ON t1.groupId = t2.groupId AND t1.created_at = t2.maxDate;

取A表中的字段,插入到B表中

INSERT INTO B(
    vendor_code, 
    organization_code, 
    item_code, 
    brand_place, 
    brand_place_id, 
    brand_belong_country
)
SELECT
    s.vendor_code,
    s.organization_code,
    s.item_code,
    s.BRAND_PLACE,
    s.BRAND_PLACE_ID,
    p.BRAND_BELONG_COUNTRY
FROM 
    A s
WHERE s.DELETE_FLAG = 0
    AND s.STATUS = 'ACTIVE';

取A表中的字段,更新到B表中

-- 1. 取 B 表 中的数据,并变成(organization_code, vendor_code, item_code)维度的数据
-- 2. 按照(organization_code, vendor_code, item_code)维度,更新到 A 表
UPDATE A rb
JOIN (
    SELECT 
        organization_code,
        vendor_code,
        item_code,
        GROUP_CONCAT(DISTINCT brand_place) AS brand_place,
        GROUP_CONCAT(DISTINCT brand_place_id) AS brand_place_id
    FROM 
        B
    GROUP BY 
        organization_code, vendor_code, item_code
) so ON rb.organization_code = so.organization_code
    AND rb.vendor_code = so.vendor_code
    AND rb.item_code = so.item_code
SET 
    rb.ATTRIBUTE9  = so.brand_place,
    rb.ATTRIBUTE10 = so.brand_place_id
Last Updated: 5/23/25, 9:59 AM
Contributors: dongyz8
Prev
概述