Pawan
Pawan

Reputation: 32331

How to get 0 from max function incase there are no records

I have a table , from which i am getting max value , but incase of no records how to get value as 0 instead of null

This is my table

CREATE TABLE IF NOT EXISTS `video_details` (
  `video_id` int(6) NOT NULL auto_increment COMMENT 'Auto Generated key',
  `video_name` varchar(50) default NULL,
  PRIMARY KEY  (`video_id`)
) ENGINE=InnoDB AUTO_INCREMENT=382 DEFAULT CHARSET=utf8;

This is my query

select max(vd.video_id) as count from video_details vd;

Upvotes: 1

Views: 29

Answers (2)

Jens
Jens

Reputation: 69460

use ifnull

select ifnull(max(vd.video_id),0) as count from video_details vd;

Upvotes: 2

Juan Carlos Oropeza
Juan Carlos Oropeza

Reputation: 48197

use COALESCE

 select COALESCE (max(vd.video_id),0) as count 
 from video_details vd;

Upvotes: 2

Related Questions