Mysql查询为空赋值为0

        SELECT
			a.id AS schoolId,
			a. NAME AS schoolName,
			a.area_id as areaId,
			a.school_type AS schoolType,
			a.school_contact AS schoolContact,
			a.contact_telphone AS contactTelphone,
			a.student_number AS studentNumber,
			DATE_FORMAT(a.account_expire_time,'%Y-%m-%d') AS accountExpireTime,
			DATE_FORMAT(a.create_time, '%Y-%m-%d') AS createTime,
			IFNULL(c.campusNum ,0) as campusNum
		FROM
			school a left join
			(
				SELECT
					count(1) as campusNum,
					b.school_id s_id
				FROM
					campus b
				WHERE
					b.is_deleted = 0
				GROUP BY
					b.school_id
			) c
		on
			a.id = c.s_id
		AND a.organization_id = 1
		AND a.is_deleted = 0

使用IFNULL函数可以完美解决。

 

你可能感兴趣的:(数据库)