MySQL各种行格式应用场景_mysql 常见使用场景

场景1:表中多条相同账户数据,根据创建日期排序取第一条。

select * from cms_client_rpq rpq

where id = (

select id from cms_client_rpq

where cif_no = rpq.cif_no

order by create_time desc

limit 1

)

场景2.primary key 不区分大小写

ALTER TABLE `cms_rpq_risk_report`

CHANGE COLUMN `accountid` `accountid` VARCHAR(50) binary primary key ;//设置为binary

场景3.将表A 中的字段col_a,更新到表B 的字段col_b,用表A的id,关联表B 的p_id

update A a,B b set b.col_b=a.col_a

where b.p_id=a.id

场景4.查找某个字符串中,某个字段出现的次数

length(accountids)-length(replace(accountids,'#','')) length

场景5.动态行转列

原表数据样式:

9bd2c0229795c89a11ca0beca3055d5d.png

将不同时间段受教育时间转成列:

aad19477769d176676d3fae030f23699.png

select

educate.user_id,

educate.user_name,

college.start_time college_start_time,

college.end_time college_end_time,

senior.start_time senior_start_time,

senior.end_time senior_end_time

from

(select distinct user_id,user_name from user_educate_base_info) educate

left join user_educate_base_info college on college.user_id=educate.user_id and college.education='college'

left join user_educate_base_info senior on senior.user_id=educate.user_id and senior.education='senior'

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值