【MySQL】MySQL 中的各种数据类型转换

Posted by 西维蜀黍 on 2020-01-31, Last Modified on 2021-11-05

Int -> varcher

将Int 转为varchar可以用 concat() 函数,比如 concat(8,’0′) 得到字符串 ’80′。

delete from core_channel;


drop procedure if exists insert_channel;

delimiter //

CREATE procedure insert_channel()
wholeblock:BEGIN
  declare str VARCHAR(255) default '';
  declare x INT default 0;
  SET x = 1;

  WHILE x <= 10 DO
    insert into core_channel (name) values (CONCAT("name",x));
    SET x = x + 1;
  END WHILE;

  select str;
END//

call insert_channel	();

Varcher -> Int

将 varchar 转为 Int 用 cast(a as signed),a为varchar类型的字符串。