0

我正在使用mysql toad,我有一个空表,如果没有找到记录,我试图返回一个字符串值我尝试了下面提到的查询,但是没有一个有效,只返回一个空行。

询问

select coalesce(stDate,'0')as StDate from tblStudents where studentNumber = 12213123;

select IFNULL(stDate,'0')as StDate from tblStudents where studentNumber = 12213123;

结果只是空字符串,列 stDate 的类型为 varchar。

4

1 回答 1

1

尝试这个

 select Cast(count(*) as char(10)) as StDate
 from tblStudents where studentNumber = 12213123;

您还可以使用 CASE 语句,如果学生编号是唯一的,则使用 max() 或 min() 函数从表中获取 stDate。

 select Cast(count(*) as char(10)) as NumRows,max(stDate) as stDate
 from tblStudents where studentNumber = 12213123;

如果你只想要一个字段,试试这样的

select 
CASE count(*)
    WHEN 0 THEN ''
    ELSE CAST(max(stDate) as char(12))
END CASE as StartDate
     from tblStudents where studentNumber = 12213123;
于 2013-05-04T02:12:19.537 回答