MySQL强制查询返回NULL

时间:2012-02-24 09:53:38

标签: php mysql null

我有以下内容:

SELECT q25, COUNT( q25 ) AS count, AVG (q1) AS q1
FROM tresults WHERE id = 'Yes' AND date = 'MARCH2010' AND q25 = '1' 
GROUP BY q25

目前,查询返回MySQL returned an empty result set (i.e. zero rows).这是正确的 - 是否可以让它返回NULL

有没有办法在PHP事件之后处理这个问题,例如:

$resultprev = mysql_query($queryprev);
if($resultprev == ''){
// do something
}

2 个答案:

答案 0 :(得分:3)

使用mysql_num_rows

$resultprev = mysql_query($queryprev);
$num_rows = mysql_num_rows($resultprev);
if($num_rows == 0){
   // 0 results !
}

或者你可以结合一个空行:

SELECT q25, COUNT( q25 ) AS count, AVG (q1) AS q1
FROM tresults WHERE id = 'Yes' AND date = 'MARCH2010' AND q25 = '1' 
GROUP BY q25

UNION

SELECT null, null as count, null as q1

答案 1 :(得分:2)

试试这个

if(mysql_affected_rows($res)) {
    //your stuff
}

或在sql中

SELECT (
    SELECT q25, COUNT( q25 ) AS count, AVG (q1) AS q1
    FROM tresults WHERE id = 'Yes' AND date = 'MARCH2010' AND q25 = '1' 
    GROUP BY q25

    UNION

    SELECT null, null, null

) AS x
LIMIT 1