---
title: "一个对字符串处理的SQL"
author: "wangmingming2008"
date: "2013-10-16T11:04:06Z"
tags: []
categories: ["数据库"]
slug: "sql"
draft: false
lastmod: "2013-10-16T11:07:16Z"
id: 1958746
url: "https://wangmingming2008.iteye.com/blog/1958746"
---

某表中有一个json格式的字段 如props里有以下字符
{"1":36,"2":54,"3":160,"4":8,"5":767680,"6":12,"7":1,"8":8,
"13":"14,17,21,20,23,22,25,24,29,28,31,30,34,35,32,33,39,96,42,43,108,40,41,107,44,116,115,114,112,125,124",
"14":{"15":"0","13":"3","14":"0","11":"3","12":"3","3":"56","2":"38","1":"56","10":"4","7":"40","6":"55","5":"57","4":"37",
"9":"6","8":"13"},"15":},
{"9":},{"11":},
{"10":},{"12":},
{"13":}],"16":160,"18":{"0":1381766801601,"1":1381823994910},
"19":8,"20":767671,"21":,
"22":{"9012":"1001,1381762836328,1,1,0,false,","9011":"1001,1381707248623,1,1,0,false,
","2021":"1001,1379343644952,1,1,0,false,
","2020":"1001,1379317060249,1,1,0,false,","9010":"1001,1381634617324,1,1,0,false,
","2022":"1001,1379469893656,1,1,0,false,","1008":"1001,1379516486264,1,1,0,false,
","1111":"1001,1380020062745,1,1,0,false,","1010":"1001,1379765171241,1,1,0,false,
","1011":"1001,1379853879986,1,1,0,false,","9000":"1001,1381543622820,1,1,0,false,"},
"23":{"3":"","1":"1"},
"24":{"0":1030990,"1":2,"2":0,"3":0},"25":,"26":402,"27":{"0":1381782823890,"1":},
"28":{"2":,"1":4,"0":1381767129747},"29":15,"30":8,"31":"","32":"","33":0,"34":,
"35":1,"36":,"37":0,"38":0,"39":],"40":,"41":0,"42":0,"43":0,"44":0,"45":0,"46":,
81063479]]],,3072]]],,0]]],,994861454]]]],],"47":,"48":0,"49":,"50":0,"51":
"52":,"53":7,"54":2,"55":,"56":50400,"57":0,"58":"","60":,"61":,"62":{"throwSeq":1},"63":,"64":{"1":{},
"2":{}},"65":,"66":,"67":{"1":,"3":0,"4":0,"5":0,"2":},"68":0,"69":50,"70":1381762800263,"71":,50,1381821984680]}


现在要查出第56个属性的值

即有如何 SQL：

select passportId,level, cast(substring(props,position(',"56":' in props)+6,position(',"57":' in props)-position(',"56":' in props)-6) as signed integer) as num,cast(substring(props,position(',"56":' in props)+6,position(',"57":' in props)-position(',"56":' in props)-6) as signed integer)/1200 h_count from t_character_info where  cast(substring(props,position(',"56":' in props)+6,position(',"57":' in props)-position(',"56":' in props)-6) as signed integer) >0;


如果在shell中分布试查出各个数据库的内容 并把内容邮件形式发出:
即把以上sql 存入到sql文件里

程序如下:
#! /bin/sh

last_date=$(date -d last-day +%Y_%m_%d);
cat /dev/null > $last_date.data
for s_ip in $(awk -F= '{print $0}' /root/action_shell/kr_ip.txt);
do
        echo ===============================start to do task===================================================;
        s=$(echo $s_ip|awk -F= '{print $1}');
        ip=$(echo $s_ip|awk -F= '{print $2}');

        echo '====================='$s'.cqkr.huayugame.com==============================================' >>/root/action_shell/$last_date.data
        out1=$(mysql -h$ip -uxxx -pxxx -Dgen -e 'source query_hounter.sql' >> /root/action_shell/$last_date.data);

        echo $out1;
        sleep 10
        echo ===============================send mail successfully===================================================;
done


/bin/mail -s 'from games.asia team tech '_$last_date  mingming.wang@renren-inc.com   games.asia< /root/action_shell/$last_date.data


运行效果如下:

=====================s1.cqkr.huayugame.com===============================
passportId	level	num	h_count
102833976	78	2400	2.0000
102835789	84	4800	4.0000
102837173	72	12000	10.0000
102837399	72	51600	43.0000
102837452	74	3600	3.0000
102839736	56	48000	40.0000
102775934	60	50400	42.0000
=====================s2.cqkr.huayugame.com==================================
passportId	level	num	h_count
102928945	76	2400	2.0000
102966633	63	72000	60.0000
102968267	64	12000	10.0000
102923416	67	1200	1.0000
102970589	57	48000	40.0000
102971607	68	28800	24.0000
102885000	69	12000	10.0000
102975125	64	24000	20.0000
102915079	69	10800	9.0000
102977473	65	42000	35.0000
=====================s3.cqkr.huayugame.com==================================
passportId	level	num	h_count
103214239	65	6000	5.0000
103048907	70	1200	1.0000
103166121	61	2400	2.0000
102906791	64	1200	1.0000
103259853	63	1200	1.0000
103268837	65	1200	1.0000
103270689	69	3600	3.0000