为此,您可以使用带有动态 sql 的存储过程。
首先,您必须创建这个存储过程:
CREATE PROCEDURE FetchColumns (IN pSchemaName char(50), IN pTableName char(50), IN pColumnLikeCondition char(50))
BEGIN
DECLARE v_cols char(200);
SELECT group_concat(COLUMN_NAME separator ' , ') INTO v_cols FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_name = pTableName
AND table_schema = pSchemaName
AND column_name LIKE pColumnLikeCondition;
SET @s = CONCAT('SELECT ',v_cols,' FROM ',pTableName );
PREPARE stmt FROM @s;
EXECUTE stmt;
END
最后,您所要做的就是调用它,如下所示:
call FetchColumns('DB', 'table', 'f%')