-1

I have a simple table now I want when I run sql statement it should replace NULL with 0 using mysql

Here is what I have tried so far

SELECT IFNULL(targetbuttonname, 0), sessions.sid, events.datetime, count(*) as num_rows,
  count(distinct sessions.sid) as sessions, 
  sum( targetbuttonname = 'kredyt' ) as num_kredyt, 
  sum(devicetype ='Computer') as num_computer from events 

Now when I run my script it still returns null

Here is DEMO demo

What is wrong with my query statement?

7

1 Answer 1

1

Use COALESCE

SELECT coalesce(NULL,0), sessions.sid, events.datetime, count(*) as num_rows, count(distinct sessions.sid) as sessions, 
  sum( targetbuttonname = 'kredyt' ) as num_kredyt, 
  sum( targetbuttonname = 'konto' ) as num_konto,
  sum( targetbuttonname = 'czat' ) as num_czat,
  sum( targetbuttonname = 'video-voice_btns' ) as num_voice,
  sum( targetbuttonname = 'video-close_btn' )  as num_close,
  sum( targetbuttonname = 'video-muted_btn' ) as num_muted,
  sum( targetbuttonname = 'video-play_btn' )  as num_play,
  sum( targetbuttonname = 'video-pause_btn' )  as num_pause,
  sum( targetbuttonname = 'video-replay_btn' ) as num_replay, 
  sum(watchtime) as num_watchtime, 
  sum(devicetype ='Computer') as num_computer from events  INNER JOIN sessions ON (events.sid =sessions.sid) WHERE events.datetime BETWEEN '2019-11-11' AND '2019-11-21'

DEMO: http://sqlfiddle.com/#!9/3cf3cf/10

IFNULL will also work:

SELECT ifnull(targetbuttonname,0), sessions.sid, events.datetime, count(*) as num_rows, count(distinct sessions.sid) as sessions, 
  sum( targetbuttonname = 'kredyt' ) as num_kredyt, 
  sum( targetbuttonname = 'konto' ) as num_konto,
  sum( targetbuttonname = 'czat' ) as num_czat,
  sum( targetbuttonname = 'video-voice_btns' ) as num_voice,
  sum( targetbuttonname = 'video-close_btn' )  as num_close,
  sum( targetbuttonname = 'video-muted_btn' ) as num_muted,
  sum( targetbuttonname = 'video-play_btn' )  as num_play,
  sum( targetbuttonname = 'video-pause_btn' )  as num_pause,
  sum( targetbuttonname = 'video-replay_btn' ) as num_replay, 
  sum(watchtime) as num_watchtime, 
  sum(devicetype ='Computer') as num_computer from events  INNER JOIN sessions ON (events.sid =sessions.sid) WHERE events.datetime BETWEEN '2019-11-11' AND '2019-11-21'

Here is the DEMO: http://sqlfiddle.com/#!9/3cf3cf/14

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for help , now when I change the date range to dates which are not in the table still return NAN, ,meaning null
meaning on console it shows null and on browser it shows NAN
just change the range of the dates like this WHERE events.datetime BETWEEN '2019-04-11' AND '2019-03-21'
Ok, but then you have to do coalesce for every column!
So, this is what you want: sqlfiddle.com/#!9/332ef1/7 ??
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.