MySQL 通過SUBSTRING_INDEX函式拆分字串為陣列單元
vim test.sh
#Define variables
MYSQL_HOME=/usr
SQL_SCRIPT_DB_CONNECTION_STRING='-uroot -p'root''
SQL_SCRIPT_PARAMETERS_LIST="'PARAM1 PARAM2 PARAM3'"
#Execute the command
#CMD_SQL_SCRIPT_EXECUTION=${MYSQL_HOME}/bin/mysql ${SQL_SCRIPT_DB_CONNECTION_STRING} -e "set @VAR1=$SQL_SCRIPT_PARAMETERS_LIST ; source ${SQL_SCRIPT_FILE_LOCATION}/${SQL_SCRIPT_NAME}.sql ;" >> ${myLogFile}
${MYSQL_HOME}/bin/mysql ${SQL_SCRIPT_DB_CONNECTION_STRING} -e "set @VAR=$SQL_SCRIPT_PARAMETERS_LIST;
set @VAR_NUM=length(@VAR)-length(REPLACE(@VAR,' ',''))+1; /* define the total number of the variables */
set @VAR_POS=0; /* define the variable position in the string */
/* define the variables' value, if the the variable doesn't exist, its value is null */
set @VAR_1 = if(@VAR_POS < @VAR_NUM,SUBSTRING_INDEX(SUBSTRING_INDEX(@VAR, ' ', @VAR_POS:=@VAR_POS+1),' ',-1),null);
set @VAR_2 = if(@VAR_POS < @VAR_NUM,SUBSTRING_INDEX(SUBSTRING_INDEX(@VAR, ' ', @VAR_POS:=@VAR_POS+1),' ',-1),null);
set @VAR_3 = if(@VAR_POS < @VAR_NUM,SUBSTRING_INDEX(SUBSTRING_INDEX(@VAR, ' ', @VAR_POS:=@VAR_POS+1),' ',-1),null);
set @VAR_4 = if(@VAR_POS < @VAR_NUM,SUBSTRING_INDEX(SUBSTRING_INDEX(@VAR, ' ', @VAR_POS:=@VAR_POS+1),' ',-1),null);
set @VAR_5 = if(@VAR_POS < @VAR_NUM,SUBSTRING_INDEX(SUBSTRING_INDEX(@VAR, ' ', @VAR_POS:=@VAR_POS+1),' ',-1),null);
set @VAR_6 = if(@VAR_POS < @VAR_NUM,SUBSTRING_INDEX(SUBSTRING_INDEX(@VAR, ' ', @VAR_POS:=@VAR_POS+1),' ',-1),null);
set @VAR_7 = if(@VAR_POS < @VAR_NUM,SUBSTRING_INDEX(SUBSTRING_INDEX(@VAR, ' ', @VAR_POS:=@VAR_POS+1),' ',-1),null);
select @VAR_1, @VAR_2, @VAR_3, @VAR_4, @VAR_5, @VAR_6, @VAR_7;"
sh test.sh
Warning: Using a password on the command line interface can be insecure.
+--------+--------+--------+--------+--------+--------+--------+
| @VAR_1 | @VAR_2 | @VAR_3 | @VAR_4 | @VAR_5 | @VAR_6 | @VAR_7 |
+--------+--------+--------+--------+--------+--------+--------+
| PARAM1 | PARAM2 | PARAM3 | NULL | NULL | NULL | NULL |
+--------+--------+--------+--------+--------+--------+--------+
#Define variables
MYSQL_HOME=/usr
SQL_SCRIPT_DB_CONNECTION_STRING='-uroot -p'root''
SQL_SCRIPT_PARAMETERS_LIST="'PARAM1 PARAM2 PARAM3'"
#Execute the command
#CMD_SQL_SCRIPT_EXECUTION=${MYSQL_HOME}/bin/mysql ${SQL_SCRIPT_DB_CONNECTION_STRING} -e "set @VAR1=$SQL_SCRIPT_PARAMETERS_LIST ; source ${SQL_SCRIPT_FILE_LOCATION}/${SQL_SCRIPT_NAME}.sql ;" >> ${myLogFile}
${MYSQL_HOME}/bin/mysql ${SQL_SCRIPT_DB_CONNECTION_STRING} -e "set @VAR=$SQL_SCRIPT_PARAMETERS_LIST;
set @VAR_NUM=length(@VAR)-length(REPLACE(@VAR,' ',''))+1; /* define the total number of the variables */
set @VAR_POS=0; /* define the variable position in the string */
/* define the variables' value, if the the variable doesn't exist, its value is null */
set @VAR_1 = if(@VAR_POS < @VAR_NUM,SUBSTRING_INDEX(SUBSTRING_INDEX(@VAR, ' ', @VAR_POS:=@VAR_POS+1),' ',-1),null);
set @VAR_2 = if(@VAR_POS < @VAR_NUM,SUBSTRING_INDEX(SUBSTRING_INDEX(@VAR, ' ', @VAR_POS:=@VAR_POS+1),' ',-1),null);
set @VAR_3 = if(@VAR_POS < @VAR_NUM,SUBSTRING_INDEX(SUBSTRING_INDEX(@VAR, ' ', @VAR_POS:=@VAR_POS+1),' ',-1),null);
set @VAR_4 = if(@VAR_POS < @VAR_NUM,SUBSTRING_INDEX(SUBSTRING_INDEX(@VAR, ' ', @VAR_POS:=@VAR_POS+1),' ',-1),null);
set @VAR_5 = if(@VAR_POS < @VAR_NUM,SUBSTRING_INDEX(SUBSTRING_INDEX(@VAR, ' ', @VAR_POS:=@VAR_POS+1),' ',-1),null);
set @VAR_6 = if(@VAR_POS < @VAR_NUM,SUBSTRING_INDEX(SUBSTRING_INDEX(@VAR, ' ', @VAR_POS:=@VAR_POS+1),' ',-1),null);
set @VAR_7 = if(@VAR_POS < @VAR_NUM,SUBSTRING_INDEX(SUBSTRING_INDEX(@VAR, ' ', @VAR_POS:=@VAR_POS+1),' ',-1),null);
select @VAR_1, @VAR_2, @VAR_3, @VAR_4, @VAR_5, @VAR_6, @VAR_7;"
sh test.sh
Warning: Using a password on the command line interface can be insecure.
+--------+--------+--------+--------+--------+--------+--------+
| @VAR_1 | @VAR_2 | @VAR_3 | @VAR_4 | @VAR_5 | @VAR_6 | @VAR_7 |
+--------+--------+--------+--------+--------+--------+--------+
| PARAM1 | PARAM2 | PARAM3 | NULL | NULL | NULL | NULL |
+--------+--------+--------+--------+--------+--------+--------+
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/26506993/viewspace-2125937/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- mysql 函式substring_index()MySql函式Index
- 陣列拆分成單引號逗號隔開的字串陣列字串
- 陣列二:使用陣列可變函式為陣列排序陣列函式排序
- sqlserver拆分字串函式 及應用SQLServer字串函式
- 7.PHP陣列和字串常用函式PHP陣列字串函式
- 陣列排序函式-php陣列函式(一)陣列排序函式PHP
- mysql函式substring_index實現split切割效果MySql函式Index
- JavaScript陣列、字串、數學函式的知識點JavaScript陣列字串函式
- MySQL 字串函式:字串擷取MySql字串函式
- MYSQL的字串函式MySql字串函式
- MySQL 字串函式大全MySql字串函式
- MySQL(四)日期函式 NULL函式 字串函式MySql函式Null字串
- 利用SQL的charindex實現字串陣列和Split函式SQLIndex字串陣列函式
- MySQL函式學習(一)-----字串函式MySql函式字串
- JavaScript 字串轉換為陣列JavaScript字串陣列
- 將字串陣列轉換為浮點數陣列字串陣列
- PHP 陣列常用函式PHP陣列函式
- 陣列處理函式陣列函式
- 陣列展平函式陣列函式
- 巧用 PHP 陣列函式PHP陣列函式
- PHP 陣列函式妙用PHP陣列函式
- Mysql字串擷取函式MySql字串函式
- MySQL字串連線函式MySql字串函式
- javascript陣列的map()函式用法簡單介紹JavaScript陣列函式
- json字串 轉換為陣列JSON字串陣列
- JavaScript將陣列轉換為字串JavaScript陣列字串
- Join方法把陣列轉為字串陣列字串
- MySQL字串函式 字串大小寫轉換MySql字串函式
- php陣列函式小結PHP陣列函式
- shell--函式與陣列函式陣列
- PHP陣列函式彙總PHP陣列函式
- 陣列常用函式彙總陣列函式
- 精通javascript 函式和陣列JavaScript函式陣列
- MySQl 擷取函式 left(),right(),substring(),substring_index() 的用法MySql函式Index
- Oracle自定義函式---按照某個分隔符拆分字串Oracle函式字串
- PHP陣列函式的時間複雜度清單PHP陣列函式時間複雜度
- 一個簡單的oracle函式返回陣列的例子Oracle函式陣列
- 形函式,等參單元,雅克比矩陣,高斯積分函式矩陣