MySQL force query to return NULL

ε祈祈猫儿з 提交于 2019-12-24 08:34:20

问题


I have the following:

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

At the moment, the query returns MySQL returned an empty result set (i.e. zero rows). which is correct - is it possible to get it to return NULL instead?

OR

Is there a way of dealing with this after the event in PHP, such as:

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

回答1:


use mysql_num_rows :

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

or you could union a null row :

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



回答2:


try this

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

or in 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


来源:https://stackoverflow.com/questions/9428680/mysql-force-query-to-return-null

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!