Jump to content

Recommended Posts

Hello everyone,

 

I am trying to force mySQL to return null values rather than no rows on my join.  So here is my scenario

 

Table 1

ID | NAME | VALUE | AnotherID
1   | blah   | 25 | 2
2   | z        | 36 | 2
3   | a        | 1 | 3
4   | b        | 2 | 2

 

Table 2

ID2 | ID | otherValue | OtherValue2
1     | 2   | b   | 4

 

Here would be my sql code

SELECT table1.*, table2.otherValue, table2.otherValue2 FROM table1
LEFT JOIN table2 ON table2.ID=table1.ID
WHERE table1.anotherID=2

 

Now I would like my result to look like the result below but instead I get 0 rows.

 

ID | NAME | VALUE | AnotherID | otherValue | otherValue2
1   | blah   | 25 | 2 | NULL   | NULL
2   | z        | 36 | 2 | b   |4
4   | b        | 2 | 2 | NULL | NULL

 

How can I do this?

 

Thank you!

weee

Link to comment
https://forums.phpfreaks.com/topic/228256-return-null-values/
Share on other sites

NULL doesn't equal anything, not even another NULL. If you attempt to select with a comparison between two fields that are NULL, they will not match. For example, if `name1` and `name2` are both NULL in `table`, and you try to:

SELECT FROM `table` WHERE `name1` = `name2`

the query will return an empty result set.

 

I'm not sure if that's contributing to the problem here, but it's something you should be aware of.

Link to comment
https://forums.phpfreaks.com/topic/228256-return-null-values/#findComment-1177841
Share on other sites

My scenario is that I have a value=NULL and that is resulting in 0 rows on my join, rather than replying back as onn on my second table.

 

With the SQL you have posted I do not see how as you are not comparing any of the columns with NULL values with any other columns.

 

Looking at it, it should give what you want it to give. Possible that the problem is something you have removed to provide a simple piece of SQL for this thread so might be worth putting the full table declares up and the actual SQL if different.

 

All the best

 

Keith

Link to comment
https://forums.phpfreaks.com/topic/228256-return-null-values/#findComment-1178237
Share on other sites

Basically I am trying to join a table that has all of the results (my left table) with a table that may have results or may not (my right table).  So I want every row to be returned on my left table regardless if the right table does not have a sufficient row to join (Ie. return back null for not finding anything).

Link to comment
https://forums.phpfreaks.com/topic/228256-return-null-values/#findComment-1178284
Share on other sites

ohh, well in that case you just have your join statement back to front (I had to build a duplicate of your sample data into my test area to notice it though ::) ), try this :

SELECT table1.id, table1.name, table1.value, table1.anotherID, table2.otherValue, table2.otherValue2 FROM 
table1 LEFT JOIN table2 ON (table1.id = table2.id)

You can use whatever WHERE your working with, I didn't need one obviously as the dataset was nice and small.

Link to comment
https://forums.phpfreaks.com/topic/228256-return-null-values/#findComment-1178545
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.