Homer_J
Homer_J

Reputation: 3323

MySQL force query to return NULL

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
}

Upvotes: 1

Views: 2988

Answers (2)

silly
silly

Reputation: 7887

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

Upvotes: 2

Manse
Manse

Reputation: 38147

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

Upvotes: 3

Related Questions