Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inserting a Zero instead of NULL while performing LEFT OUTER JOIN in MYSQL

Tags:

database

mysql

Is there a way to insert a zero instead of NULL while performing LEFT OUTER JOIN on two tables?

Suppose I have a query like this:

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`

Correct me if I'm wrong

like image 909
Crocode Avatar asked Nov 08 '12 05:11

Crocode


People also ask

How do I replace NULL with 0 in SQL?

UPDATE [table] SET [column]=0 WHERE [column] IS NULL; Null Values can be replaced in SQL by using UPDATE, SET, and WHERE to search a column in a table for nulls and replace them.

How do you change the NULL values in a join?

ISNULL() function, CASE statement, COALESCE() function can be used to replace null values in a SQL Server table. To replace null with specified replacement value, we can use any of the following: ISNULL() function.

Can LEFT join have NULL values?

The SQL LEFT JOIN returns all rows from the left table, even if there are no matches in the right table. This means that if the ON clause matches 0 (zero) records in the right table; the join will still return a row in the result, but with NULL in each column from the right table.

How do NULL values behave in left outer join?

A left outer join displays the null value in the first table. The results make it difficult to distinguish a null in the data from a null that represents a failure to join. When null values are present in data being joined, it is usually preferable to omit them from the results by using a regular join.


2 Answers

use ISNULL(field, 0) this will insert zero if the field is NULL

like image 114
donebizkit Avatar answered Sep 20 '22 05:09

donebizkit


CASE WHEN a.fieldname IS NULL 
       THEN 0
       ELSE a.fieldname
END AS fieldname
like image 21
sel Avatar answered Sep 18 '22 05:09

sel