IFNULL(expr1,expr2)
如果expr1不是NULL,IFNULL()返回expr1,否则它返回expr2。IFNULL()返回一个数字或字符串值,取决于它被使用的上下文环境。
mysql> select IFNULL(1,0);
-> 1
mysql> select IFNULL(0,10);
-> 0
mysql> select IFNULL(1/0,10);
-> 10
mysql> select IFNULL(1/0,'yes');
-> 'yes'
IF(expr1,expr2,expr3)
如果expr1是TRUE(expr1<>0且expr1<>NULL),那么IF()返回expr2,否则它返回expr3。IF()返回一个数字或字符串值,取决于它被使用的上下文。
mysql> select IF(1>2,2,3);
-> 3
mysql> select
IF(1<2,'yes','no');
-> 'yes'
=======================================
MySQL函数之STRCMP()
- STRCMP(expr1,expr2)
若所有的字符串均相同,则返回STRCMP(),若根据当前分类次序,第一个参数小于第二个,则返回 -1,其它情况返回 1 。
mysql> SELECT STRCMP('text', 'text2');
-> -1
mysql> SELECT STRCMP('text2', 'text');
-> 1
mysql> SELECT STRCMP('text', 'text');
-> 0
在执行比较时,STRCMP() 使用当前字符集。这使得默认的比较区分大小写,当操作数中的一个或两个都是二进制字符串时除外。
========================================================================
举例
SELECT
DISTINCT c.id AS crs_code,
c.name AS crs_name,
c.score,
ci.cover_img_url,
ci.crs_introduction,
cc.name,
usr.real_name,
usr.nick_name,
usr.email,
usrp.phone,
IFNULL(SUM(playcount),
0) AS seenums
FROM
y_course c
LEFT JOIN
y_crs_info ci
ON c.id = ci.crs_id
LEFT JOIN
y_school s
ON c.sch_id = s.id
LEFT JOIN
y_crs_catagory cc
ON c.cat_id = cc.id
LEFT JOIN
y_crs_team tt
ON tt.crs_id = c.id
LEFT JOIN
y_user usr
ON usr.id = tt.team_usr_id
LEFT JOIN
y_usr_profile usrp
ON usr.id = usrp.usr_id
LEFT JOIN
y_crs_file cf
ON cf.crs_id = c.id
LEFT JOIN
y_video v
ON v.vid = cf.url
WHERE
1 = 1
AND c.status = 1
AND cf.type = 0
AND c.id NOT IN(
68
)
AND (
c.is_yl_visiable = 1
OR c.sch_id = '117'
)
AND tt.team_dict_id = (
SELECT
dcc.id
FROM
y_dict dcc
WHERE
dcc.name='主讲教师'
)
GROUP BY
crs_code
ORDER BY
seenums DESC LIMIT 0,
10