Jump to content

Recommended Posts

I'm not really too sure which forum to post in, so forgive me if I should of asked this in the sql forum.

 

The below code outputs...

 

4 iron

38.0000,0.0000,72.0000,0.0000,0.0000,0.0000,0.0000,0.0000,0.0000,0.0000,0.0000,0.0000,

 

whereas i would now like to change either the PHP or SQL, so that each 0.0000 reads 'null'

 

How can i use the IFNULL keyword if the returned data type is different to the original data type?

(if i can't then what are my alternatives?)

 

How can I achieve the same result using php?

I'm guessing I would need some kind of foreach $row as $avscore statement aswell as an if statement, however im' not certain how to piece everything together.

 

 

echo "4 iron" . "<br/>" ;
$sql= "SELECT month_table.month , IFNULL(AVG( yard ), 0) AS avscore FROM month_table LEFT JOIN `snag` ON month_table.month =
 MONTH(date)AND club = '4 iron' AND user_id = $user_id AND YEAR = $year GROUP BY `month`";
$result=mysql_query($sql);
while($row = mysql_fetch_array($result) )
{$avscore = $row['avscore']; echo $avscore.',';}

Link to comment
https://forums.phpfreaks.com/topic/273961-if-row-result-is-0-then-swap-for-null/
Share on other sites

I've tried several combinations similar to the command below. basically Im trying to incorporate the NULLIF whilst still keeping 'AVG yard' to display the non-null average values;

 

SELECT month_table.month , AVG(yard), NULLIF(yard,0) AS avscore FROM month_table LEFT JOIN `snag` ON month_table.month =
MONTH(date)AND club = '3 iron' AND user_id = $user_id AND YEAR = $year GROUP BY `month` 

 

This returns ,,,,,,,,,0;

lol,

 

I have a table (month_table) with a single column (month) listing each month of the year.

I have a table (snag) with columns (yard, club, user_id, date, year...

 

I'm trying to;

 

1) display the average yard for each month of the year

2) display null where any average yard equals zero

Thankyou. So if I went down the php route, I would end up with somthing like..

 

<?php
$sql= "SELECT month_table.month , AVG( yard ) AS avscore FROM month_table LEFT JOIN `snag` ON month_table.month =
 MONTH(date)AND club = '3 iron' AND user_id = $user_id AND YEAR = $year GROUP BY `month` ";
$result=mysql_query($sql);
while($row = mysql_fetch_array($result) ){$avscore = $row['avscore']. ',' ;
echo(($avscore) ? $avscore : 'NULL');
}
?>

 

NB: removing the ifnull from avg?

If it's the world "null" then that has nothing to do with SQL NULLs, so IFNULL() or NULLIF() do not apply.

 

You could try something like the query below or do it in PHP as sweetheart Jessica suggested.

 

SELECT month_table.month , IF(AVG(yard), AVG(yard), 'NULL') AS avscore
FROM month_table
LEFT JOIN `snag` ON month_table.month = MONTH(date)
   AND club = '3 iron'
   AND user_id = $user_id
   AND YEAR = $year
GROUP BY `month`

 

edit: IMHO "NULL" is not particularly user-friendly, better would be "NONE", "NOT AVAILABLE" or "NO VALUE" or similar

Edited by Barand

Thank you for this Barand and Jessica! I'm using null as this is a requirement of the highcharts plugin I am using, but that has been noted for future use.

 

In your opinion, what is the best way to learn SQL.... eg. Book vs trial and error vs w3school tutorials etc. ?

My introduction was a FoxPro manual. I remember there was a schema diagram showing the relationships between the various tables (area, salesman, customer, orders, invoice, invoiceItem, products) and a series of questions such as finding those salesmen who had sold product 1 but not product 2 etc. Each question had an example SQL solution.

 

After that it's been trial and error. If a query performs slowly then experiment to improve it. Also reading the manuals and getting to know the internal functions available. I've also learned from the forum here over the years, both from the replies of experienced members and by tackling new problems raised by others.

 

That was the programming side; I was formally trained in the analysis of data requirements, data normalization, modelling data relationships and database design.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.