您在示例中的查询:
create table a ( i tinyint, b char(5));
SELECT
COLUMN_NAME,
case DATA_TYPE
when 'tinyint' then 'Yes'
else 'No'
end
FROM
INFORMATION_SCHEMA.COLUMNS
WHERE
table_name = 'a';
结果
已编辑,因为 OP 失去了信心。
嗨大人,相信我你不想要!好的...欢迎来到动态 sql 的阴暗面:
create table a ( i tinyint, b char(5));
insert into a values (1,'si'),(0,'no');
SELECT @a :=
concat(
'select ',
group_concat(
case DATA_TYPE
when 'tinyint' then concat(
'if( ' ,
COLUMN_NAME ,
' = 0, \'No\', \'Yes\' )'
)
else COLUMN_NAME
end
),
' from ',
table_name ,
';'
)
FROM
INFORMATION_SCHEMA.COLUMNS
WHERE
table_name = 'a';
PREPARE stmt FROM @a;
EXECUTE stmt;
结果
| IF( I = 0, 'NO', 'YES' ) | B |
---------------------------------
| Yes | si |
| No | no |