mysql【case when,IF,locate,substring_index】函数处理拆分数据

承接上篇博客,如果想要数据更加的完美,可以这样去处理。入口

思想:将前面带的别名去掉,仅展示真实的数据,如果数据没有别名直接展示不需要再去处理。

CASE WHEN ELSE END方式处理数据(推荐):

SELECT
	t.targetColumn,
	CASE
WHEN locate(
	'.',
	substring_index(
		substring_index(
			t.sourceColumn,
			'/',
			b.help_topic_id + 1
		),
		'/' ,- 1
	)
) > 0 THEN
	substring_index(
		substring_index(
			substring_index(
				t.sourceColumn,
				'/',
				b.help_topic_id + 1
			),
			'/' ,- 1
		),
		'.' ,- 1
	)
ELSE
	substring_index(
		substring_index(
			t.sourceColumn,
			'/',
			b.help_topic_id + 1
		),
		'/' ,- 1
	)
END AS sourceColumn
FROM
	syjxdata t
JOIN mysql.help_topic b ON b.help_topic_id < (
	length(t.sourceColumn) - length(
		REPLACE (t.sourceColumn, '/', '')
	) + 1
)
WHERE
	1 = 1
AND t.sourcetable IS NOT NULL
AND t.sourcesystem IS NOT NULL
AND t.sourceColumn IS NOT NULL
AND t.sourceColumn <> ''
ORDER BY
	t.targetColumn

IF(str,true,false)函数处理方式(短语句推荐)


SELECT
	t.targetColumn,

IF (
	locate(
		'.',
		substring_index(
			substring_index(
				t.sourceColumn,
				'/',
				b.help_topic_id + 1
			),
			'/' ,- 1
		)
	) > 0,
	substring_index(
		substring_index(
			substring_index(
				t.sourceColumn,
				'/',
				b.help_topic_id + 1
			),
			'/' ,- 1
		),
		'.' ,- 1
	),
	substring_index(
		substring_index(
			t.sourceColumn,
			'/',
			b.help_topic_id + 1
		),
		'/' ,- 1
	)
)
FROM
	syjxdata t
JOIN mysql.help_topic b ON b.help_topic_id < (
	length(t.sourceColumn) - length(
		REPLACE (t.sourceColumn, '/', '')
	) + 1
)
WHERE
	1 = 1
AND t.sourcetable IS NOT NULL
AND t.sourcesystem IS NOT NULL
AND t.sourceColumn IS NOT NULL
AND t.sourceColumn <> ''
ORDER BY
	t.targetColumn

结果
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值