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 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺