douju2331 2014-04-30 15:54
浏览 1007
已采纳

将TINYINT(1)转换为字符串

I want to convert a TINYINT(1) value to string. Let's say if subscription paid is equal to 1 (or true) then the outcome will be "yes" when I retrieve the value from my MySQL database to my .php file. If subscription paid = 0 (or false) the the outcome will be "no".

Is there a way to convert a TINYINT(1) value to a string containing my words of choice?

I am sorry if I am being indirect or if more information about the issue is required

  • 写回答

3条回答 默认 最新

  • dp13668681869 2014-04-30 16:02
    关注

    Given that MySQL does not provide any decent mechanism to prevent inserting -1 or 33 into a TINYINT column, I'd simply test against zero/non-zero:

    SELECT CASE
        WHEN paid<>0 THEN 'Yes'
        WHEN paid=0 THEN 'No'
        ELSE 'Unknown'
    END is_paid
    FROM suscription
    

    The question is also tagged as PHP. I think it's cleaner to use native types as much as possible:

    if (is_null($row['paid']) {
        $paid = null;
    }else{
        $paid = $row['paid']!=0;
    }
    

    Getting the right string when displaying should be straightforward.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 一道python难题
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度