使用mysql
自身的user
表,拼接其中的User
字段的值砾嫉,使用GROUP_CONCAT
函數(shù)幼苛。
# 使用mysql表
USE mysql;
# 查詢`user`
SELECT *
FROM `user`;
# 結(jié)果
Host User Select_priv Insert_priv Update_priv Delete_priv Create_priv Drop_priv Reload_priv Shutdown_priv Process_priv File_priv Grant_priv References_priv Index_priv Alter_priv Show_db_priv Super_priv Create_tmp_table_priv Lock_tables_priv Execute_priv Repl_slave_priv Repl_client_priv Create_view_priv Show_view_priv Create_routine_priv Alter_routine_priv Create_user_priv Event_priv Trigger_priv Create_tablespace_priv ssl_type ssl_cipher x509_issuer x509_subject max_questions max_updates max_connections max_user_connections plugin authentication_string password_expired password_last_changed password_lifetime account_locked
localhost root Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y (BLOB) 0 bytes (BLOB) 0 bytes (BLOB) 0 bytes 0 0 0 0 mysql_native_password N 2018-12-29 10:46:47 N
localhost mysql.session N N N N N N N N N N N N N N N Y N N N N N N N N N N N N N (BLOB) 0 bytes (BLOB) 0 bytes (BLOB) 0 bytes 0 0 0 0 mysql_native_password *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE N 2018-12-29 10:41:42 Y
localhost mysql.sys N N N N N N N N N N N N N N N N N N N N N N N N N N N N N (BLOB) 0 bytes (BLOB) 0 bytes (BLOB) 0 bytes 0 0 0 0 mysql_native_password *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE N 2018-12-29 10:41:42 Y
# 拼接`user`的值
SELECT GROUP_CONCAT(`User` separator ':') AS user
FROM `user`;
# 結(jié)果
user
mysql.session:mysql.sys:root
# 解析绿饵,GROUP_CONCAT(`列` separator '分隔符')恢氯,`separator`默認(rèn)為','
# 進(jìn)一步拼接,每個user的值都加上雙引號溯捆,如:"A","B","C"這個形式
SELECT GROUP_CONCAT(CONCAT('"', `User`, '"') separator ',') AS user
FROM `user`;
# 結(jié)果
user
"mysql.session","mysql.sys","root"
# 解析
在GROUP_CONCAT函數(shù)內(nèi)再使用CONCAT(str, str, str)配并,其中第一個參數(shù)為左邊的字符括荡,中間的為目標(biāo)列(column),第三個參數(shù)為右邊的字符溉旋。