您好,登錄后才能下訂單哦!
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 | +--------+--------+--------+--------+--------+--------+--------+
看完上述內容,你們掌握MySQL 中怎么通過SUBSTRING_INDEX函數拆分字符串為數組單元的方法了嗎?如果還想學到更多技能或想了解更多相關內容,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。