在MYSQL中执行LEFT OUTER JOIN时插入零而不是NULL

时间:2012-11-08 05:23:04

标签: mysql database

在两个表上执行LEFT OUTER JOIN时,有没有办法插入零而不是NULL?

假设我有这样的查询:

SELECT * FROM
(SELECT uID from Class) T1
LEFT OUTER JOIN
(SELECT pID from University) T2
ON T1.uID = T2.pID
CASE WHEN T1.uID IS NULL
    THEN 0
ELSE T1.uID
END AS uID`

如果我错了,请纠正我

3 个答案:

答案 0 :(得分:11)

使用ISNULL(字段,0)如果字段为NULL,则将插入零

答案 1 :(得分:9)

CASE WHEN a.fieldname IS NULL 
       THEN 0
       ELSE a.fieldname
END AS fieldname

答案 2 :(得分:2)

类似于: -

SELECT * FROM
(SELECT ifnull(uID,0) as uID from Class) T1
   LEFT OUTER JOIN
 (SELECT ifnull(pID,0) as pID from University) T2
   ON T1.uID = T2.pID