结果:
说明:将多个字符串连接成一个字符串,但是可以一次性指定分隔符~(concat_ws就是concat with separator)
GROUP_CONCAT([DISTINCT] expr [,expr ...] [ORDER BY {unsigned_integer | col_name | expr} [ASC | DESC] [,col_name ...]] [SEPARATOR str_val])
说明:
DISTINCT:去除重复值
expr [,expr ...]:一个或多个字段(或表达式)
ORDER BY {unsigned_integer | col_name | expr} [ASC | DESC] [,col_name ...]:根据字段或表达式进行排序,可多个
SEPARATOR str_val:分隔符(默认为英文逗号)
group_concat()函数在处理大数据的时候,会发现内容被截取了
其实MYSQL内部对这个是有设置的,默认不设置的长度是1024,如果我们需要更大,就需要手工去修改配置文件
mysql不能直接运行动态sql语句的,但是可以写在存储过程中,然后调用过程执行,具体如下:
(1)定义存储过程,示例CREATE DEFINER=`root`@`localhost` PROCEDURE `query_extra_table(table_index varchar(255) );
BEGIN
SET @sql = concat("select * from tbl_order_", table_index)
PREPARE stmt FROM @sql
EXECUTE stmt
END
(2)调用存储过程:
CALL query_extra_table('483')
DROP PROCEDURE IF EXISTS `p_create_view`CREATE PROCEDURE p_create_view()
BEGIN
DECLARE tableName VARCHAR (100)
DECLARE stopFlag INT
DECLARE sql1 VARCHAR (1000)
DECLARE cursor_name CURSOR FOR SELECT TABLE_NAME FROM information_schema.tables WHERE TABLE_NAME LIKE 'test_2014%'
DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET stopFlag=1
SET @sql1='create view test as select id,name from '
OPEN cursor_name
REPEAT
FETCH cursor_name INTO tableName
SELECT CONCAT(@sql1,tableName,' UNION ALL SELECT id,name FROM ') INTO @sql1 FROM DUAL
UNTIL stopFlag END REPEAT
CLOSE cursor_name
SELECT SUBSTR(@sql1,1,LENGTH(@sql1)-31) INTO @sql1 FROM DUAL
PREPARE step FROM @sql1
EXECUTE step
END
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)