简体   繁体   中英

MySql Toad if now rows return value

I am using mysql toad , i have a table which is empty and i am trying to return a string value if no records are found i have tried the under mention queries however none works, only an empty row is returned.

Query

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

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

The results are only empty strings the column stDate is of type varchar.

Try this

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

You can also use the CASE statement, and if the Student Number is unique, the max() or min() functions to get the stDate from the table.

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

If you only want a single field, try something like this

select 
CASE count(*)
    WHEN 0 THEN ''
    ELSE CAST(max(stDate) as char(12))
END CASE as StartDate
     from tblStudents where studentNumber = 12213123;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM