如何在查询为空时显示某些内容?

时间:2013-02-04 08:02:33

标签: mysql sql

如果查询完成,我的代码显示“1”,查询为空时如何显示“0”。

SELECT  1 as 'access' 
FROM  `mytable` 
WHERE  `name` = 'Prophet Muhammad' and id = 1;

2 个答案:

答案 0 :(得分:3)

使用COUNT

SELECT  (COUNT(*) >= 1) as 'access' 
FROM    mytable
WHERE   name = 'Prophet Muhammad' and 
        id = 1;

答案 1 :(得分:1)

你可以使用它,当查询返回至少一行时显示1,否则显示0:

SELECT EXISTS (SELECT Null
               FROM  `mytable` 
               WHERE  `name` = 'Prophet Muhammad' and id = 1) as Status