UnKnown UnKnown - 16 days ago 8
MySQL Question

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;

Answer

use COALESCE

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