创建SQL表,如何将BIT条目转换为文本YES或NO?

时间:2014-01-06 12:49:09

标签: mysql sql

非常新的SQL并尝试设置用户表,我有一个财务援助复选框,输出BIT 0或1,理想情况下我想要做的是将此值转换为YES或NO取决于价值。现在我不完全确定如何做到这一点,我已经尝试添加

SELECT CASE `financial_aid`
         WHEN 1 THEN 'Yes' 
         WHEN 0 THEN 'No' 
       END As `financial_aid`
FROM `users`;

目前这并没有解决我的问题,有人可以建议我如何解决这个问题吗?

SQL

CREATE TABLE IF NOT EXISTS `users` (
  `User_id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
  `player_name` varchar(40) NOT NULL,
  `gender` varchar(10) NOT NULL,
  `dob` date NOT NULL,
  `parent_name` varchar(40) NOT NULL,
  `parent_email` varchar(40) NOT NULL,
  `parent_phone` varchar(40) NOT NULL,
  `experience` varchar(40) NOT NULL,
  `financial_aid` bit NOT NULL,
  `registration_date` datetime NOT NULL,
  PRIMARY KEY (`User_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=10;

SELECT CASE `financial_aid`
          WHEN 1 THEN 'Yes'
          WHEN 0 THEN 'No' 
       END As `financial_aid` 
FROM `users`;

1 个答案:

答案 0 :(得分:2)

尝试IF(true, x, y)功能...

SELECT IF(`financial_aid`, 'Yes', 'No') As `financial_aid` FROM `users`;

或者只是

SELECT case `financial_aid`
         WHEN TRUE 'Yes'
         ELSE 'No' END
        As `financial_aid`
FROM `users`;