2015-12-28 43 views
-1

我执行下面通过PHP我的数据库查询中设置空结果迭代,如何通过在PHP

SELECT count(*) as total,ifnull(status,1000) as status from xyz 
WHERE user_id in(0) and ifnull(status,1000) in (1) 
GROUP BY ifnull(status,1000) 

后,我迭代通过

foreach($result as $r) 
{ 
    if($r->status == 1) 
     // Blobk of code 
} 

但问题是,有没有1状态的行,这就是为什么它没有执行代码块。

我希望这段代码被执行。 由于$result为空,因此截至目前尚未执行。

+0

帮助你到底是什么? –

+0

你可以发布你期望的输出吗 –

+0

你的查询是错误的 –

回答

0

希望这将帮助其他人在未来, 这就是我一直在寻找:

SELECT 
      ifnull(status,1000) as status, 
      CASE WHEN EXISTS (SELECT no FROM detail_master b WHERE ifnull(b.status,1000) in (1) and b.id in(0)) 
      THEN count(*) 
      ELSE 0 
      END AS total 
      FROM detail_master where ifnull(status,1000) in (1) and av_status = 1 group by ifnull(b.status,1000) 

输出这个查询的是:

status  total 
    1   0 

总计为0,即使没有数据礼物ID 0