MySql Toad如果现在行返回值

时间:2013-05-04 02:09:00

标签: mysql toad coalesce ifnull

我正在使用 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。

1 个答案:

答案 0 :(得分: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;