MySQL一个简单SQL语句

erytbc 2009-07-09 11:40:29
表A有字段 id, List ,
数据形如下:

id List
1 Mon=1;DE=22;BO=33;K=jin;
2 Mon=210;DE=22;BO=23;k=P;
3 null
4 Mon=12
5 ''
6 DE=1;

求SQL语句得到:
id Mon
1 1
2 210
3 0
4 12
5 0
6 0

谢谢。
...全文
40 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
erytbc 2009-07-09
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 xingzhiasa 的回复:]
这个……没看懂……你想要的得到的数据……那啥……能再说明白点不?
[/Quote]说明你没认真看
iabswfg858 2009-07-09
  • 打赏
  • 举报
回复
up
jiangshun 2009-07-09
  • 打赏
  • 举报
回复

--> 测试时间:2009-07-09 11:44:19
--> 我的淘宝: http://shop36766744.taobao.com/

if object_id('[A1]') is not null drop table [A1]
create table [A1]([id] int,[List] varchar(24))
insert [A1]
select 1,'Mon=1;DE=22;BO=33;K=jin;' union all
select 2,'Mon=210;DE=22;BO=23;k=P;' union all
select 3,null union all
select 4,'Mon=12' union all
select 5,null union all
select 6,'DE=1;'


select ID,
mon=case when charindex('Mon',List)>0
then isnull(substring(List+';',charindex('Mon=',List+';')+4,charindex(';',List+';')-charindex('Mon=',List+';')-4),0)
else 0 end
from [A1]

/*

ID mon
----------- -----------
1 1
2 210
3 0
4 12
5 0
6 0

(所影响的行数为 6 行)
*/
xingzhiasa 2009-07-09
  • 打赏
  • 举报
回复
这个……没看懂……你想要的得到的数据……那啥……能再说明白点不?
jiangshun 2009-07-09
  • 打赏
  • 举报
回复

--> 测试时间:2009-07-09 11:44:19
--> 我的淘宝: http://shop36766744.taobao.com/

if object_id('[A1]') is not null drop table [A1]
create table [A1]([id] int,[List] varchar(24))
insert [A1]
select 1,'Mon=1;DE=22;BO=33;K=jin;' union all
select 2,'Mon=210;DE=22;BO=23;k=P;' union all
select 3,null union all
select 4,'Mon=12' union all
select 5,null union all
select 6,'DE=1;'

select ID,mon=isnull(substring(List+';',charindex('=',List+';')+1,charindex(';',List+';')-charindex('=',List+';')-1),0) from [A1]

/*
ID mon
----------- -------------------------
1 1
2 210
3 0
4 12
5 0
6 1

(所影响的行数为 6 行)

*/
drop table [A1]
ACMAIN_CHM 2009-07-09
  • 打赏
  • 举报
回复
感觉不如你取出数据集到datatable中处理反而方便。

mysql> select * from t_smallkonrad;
+----+--------------------------+
| id | list |
+----+--------------------------+
| 1 | Mon=1;DE=22;BO=33;K=jin; |
| 2 | Mon=210;DE=22;BO=23;k=P; |
| 3 | NULL |
| 4 | Mon=12 |
| 5 | |
| 6 | DE=1; |
+----+--------------------------+
6 rows in set (0.00 sec)

mysql>
mysql> select id,if(ifnull(instr(list,'Mon='),0)>0,
-> SUBSTR(list,instr(list,'Mon=')+4,
-> if(LOCATE(';',list,instr(list,'Mon='))>0,
-> LOCATE(';',list,instr(list,'Mon=')),
-> 10000
-> )-instr(list,'Mon=')-4
-> ),
-> 0) as Mon
-> from t_smallkonrad;
+----+------+
| id | Mon |
+----+------+
| 1 | 1 |
| 2 | 210 |
| 3 | 0 |
| 4 | 12 |
| 5 | 0 |
| 6 | 0 |
+----+------+
6 rows in set (0.00 sec)

mysql>

62,040

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧