Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL: join table showing null value as well

Tags:

sql

mysql

i have table like this:

table1:

id | item_name | entered_by | modify_by
1  | banana    |     2      |    1
2  | apple     |     4      |    3
3  | orance    |     1      |    1
4  | pineapple |     5      |    3
5  | grape     |     6      |    1

table2:

id | username 
1  | admin
2  | jack
3  | danny
4  | dummy
5  | john
6  | peter

the query is work fine to select if the entered_by or modify_by do have value:

SELECT t1.id, t1.item_name,
  t2enteredBy.username enteredBy,
  t2modifyBy.username modifyBy
FROM table1 t1
JOIN table2 t2enteredBy ON t1.entered_by = t2enteredBy.id
JOIN table2 t2modifyBy ON t1.modify_by = t2modifyBy.id

problem: if one of the modifiy_by or entered_by field have null value, the row is now showing out, i need it to show it out as '-' if it has null value rather than hide the row completely.

SQLFIDDLE HERE

like image 712
Teddybugs Avatar asked Oct 31 '13 07:10

Teddybugs


People also ask

How joins work with NULL values in SQL?

The result of a join of null with any other value is null. Because null values represent unknown or inapplicable values, Transact-SQL has no basis to match one unknown value to another. You can detect the presence of null values in a column from one of the tables being joined only by using an outer join.

Why is my left join returning 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.

Do joins work on NULL values?

As we have seen from the above examples joining NULL values does not work. Even though you have two NULL values SQL Server does not treat these as the same value. Internally a value of NULL is an unknown value and therefore SQL Server does not equate an unknown value being equal to another unknown value.

What happens if join column is NULL?

If you attempt to join tables, and some of the columns contain null values, the null records will not be included in the resulting joined table.

Which join includes nulls?

In SQL Full Outer Join, all rows from both the tables are included. If there are any unmatched rows, it shows NULL values for them.


2 Answers

Try this out:

SELECT t1.id, t1.item_name,
  COALESCE(t2enteredBy.username, '-') enteredBy,
  COALESCE(t2modifyBy.username, '-') modifyBy
FROM table1 t1
LEFT JOIN table2 t2enteredBy ON t1.entered_by = t2enteredBy.id
LEFT JOIN table2 t2modifyBy ON t1.modify_by = t2modifyBy.id

Fiddle here.

You need a left join to return those rows with null values. Then the coalesce will make sure that they are replaced by the given string if they are null.

like image 69
Mosty Mostacho Avatar answered Oct 04 '22 22:10

Mosty Mostacho


Try this - use LEFT JOIN instead of JOIN

SELECT t1.id, t1.item_name,ifnull(t2enteredBy.username,'-') enteredBy,
  ifnull(t2modifyBy.username,'-') modifyBy
FROM table1 t1
LEFT JOIN table2 t2enteredBy ON t1.entered_by = t2enteredBy.id
LEFT JOIN table2 t2modifyBy ON t1.modify_by = t2modifyBy.id

SQL Fiddle Here

like image 25
Suhel Meman Avatar answered Oct 04 '22 20:10

Suhel Meman