473,386 Members | 1,810 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Left Join Not Returning All Rows

Hello All,

Before this I'd never beefore needed to use the "LEFT JOIN" command, and unfortunately it's giving me some problems.

I have three tables setup as follows (note they are test tables for a prototype of a larger project):

user_table:
| UserID | Username | Password|
| 1 | julian | abcdef |
| 2 | test | test |


file_table:
| FileID | UserID | File |
| 1 | 1 | file1.txt|
| 2 | 1 | file2.txt |
| 3 | 2 | file2.txt |

list_table:
| ListID | FileName |
| 1 | file1.txt |
| 2 | file2.txt |
| 3 | file3.txt |

I want to run a query that will display all rows of "FileName" on the second column, and "FileAccess" on the second column IF FileAccess is equal to FileName and UserID in TableB is equal to a variable which is given a value in a previous part of my program. (For simplicity sake it has been given the value two.

This is my query:

Expand|Select|Wrap|Line Numbers
  1. sql = "SELECT file_list.FileName, file_table.FileAccess FROM file_list "
  2. sql = sql & "LEFT JOIN file_table ON file_list.FileName = file_table.FileAccess "
  3. sql = sql & "WHERE file_table.UserID =  2"
  4.  
This is what it returns:

FileName | FileAccess
file2.txt | file2.txt

This is what I WANT it to return:
FileName | FileAccess
file1.txt |
file2.txt | file2.txt

I've tried all types of variations of the query and even tried to make it a right join all without success. Any help with the query will be greatly appreciated.
Oct 29 '06 #1
7 2088
For the file_table table the last column should be titled "FileAccess" so that the SQL query makes sense.

Thanks in advance,
Julian
Oct 29 '06 #2
scripto
143 100+
try changing your SQL to read

"WHERE file_table.UserID = 2 or file_table.UserID = null"
Oct 30 '06 #3
try changing your SQL to read

"WHERE file_table.UserID = 2 or file_table.UserID = null"
Hello scripto,

It still returns:

FileName | FileAccess
file2.txt | file2.txt

For some reason it's not displaying file1.txt from "FileName" with null for the FileAccess row.

Any further help would be appreciated,
Julian
Oct 30 '06 #4
scripto
143 100+
then drop the where clause.
Oct 30 '06 #5
iam_clint
1,208 Expert 1GB
this is how it works


when you left join or left outer join

say this is your tables
Expand|Select|Wrap|Line Numbers
  1. test1 (table)
  2. id      message
  3. 1       hi
  4. 2       this
  5. 3       is
  6. 4       odd
  7.  
  8. test2(table)
  9. id      othermessage
  10. 1       crap
  11. 2       dude
  12. 3       orange
  13. 4       cookie
  14.  
  15. SELECT * FROM test1 t1
  16.   LEFT OUTER JOIN test2 t2 ON t1.id = t2.id
  17.   WHERE t1.id = 2
  18.  
  19. result would look something like this
  20.  
  21. id    message   othermessage
  22. 2     this      dude
  23.  
  24. if you take the where off you would get something like this
  25.  
  26. id    message    othermessage
  27. 1     hi         crap
  28. 2     this       dude
  29. 3     is         orange
  30. 4     odd        cookie
  31.  
Oct 30 '06 #6
then drop the where clause.
If I drop the Where clause then it would return the FileAccess values of all users, instead of just the user I specify.
Oct 30 '06 #7
scripto
143 100+
Hello All,

Before this I'd never beefore needed to use the "LEFT JOIN" command, and unfortunately it's giving me some problems.

I have three tables setup as follows (note they are test tables for a prototype of a larger project):

user_table:
| UserID | Username | Password|
| 1 | julian | abcdef |
| 2 | test | test |


file_table:
| FileID | UserID | File |
| 1 | 1 | file1.txt|
| 2 | 1 | file2.txt |
| 3 | 2 | file2.txt |

list_table:
| ListID | FileName |
| 1 | file1.txt |
| 2 | file2.txt |
| 3 | file3.txt |

I want to run a query that will display all rows of "FileName" on the second column, and "FileAccess" on the second column IF FileAccess is equal to FileName and UserID in TableB is equal to a variable which is given a value in a previous part of my program. (For simplicity sake it has been given the value two.

This is my query:

Expand|Select|Wrap|Line Numbers
  1. sql = "SELECT file_list.FileName, file_table.FileAccess FROM file_list "
  2. sql = sql & "LEFT JOIN file_table ON file_list.FileName = file_table.FileAccess "
  3. sql = sql & "WHERE file_table.UserID =  2"
  4.  
This is what it returns:

FileName | FileAccess
file2.txt | file2.txt

This is what I WANT it to return:
FileName | FileAccess
file1.txt |
file2.txt | file2.txt

I've tried all types of variations of the query and even tried to make it a right join all without success. Any help with the query will be greatly appreciated.
ok one more shot - change the join like this:

LEFT JOIN file_table ON file_list.ListID = file_table.FileID "
Oct 30 '06 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

13
by: StealthBananaT | last post by:
My database has two tables - films has 10,000 records and reviews has 20,000 records. Whenever I try to list all the films and the count of its reviews, MySQL locks and I have to restart the...
0
by: Soefara | last post by:
Dear Sirs, I am experiencing strange results when trying to optimize a LEFT JOIN on 3 tables using MySQL. Given 3 tables A, B, C such as the following: create table A ( uniqueId int not...
1
by: Jamie Burns | last post by:
Hello, I am trying to perform a LEFT JOIN on a table which may or may not have matching rows. If there are no matching rows, it returns NULL's for all the missing fields. Is there anyway of...
1
by: Quarco | last post by:
Hi, Suppose I have a query like: SELECT products.name AS product, SUM(IF(stock.invoice=0,1,0)) AS in_stock, SUM(IF(shopcart.status=1,1,0)) AS reserved FROM products LEFT JOIN stock ON...
4
by: jbm05 | last post by:
Hi, I'm curious about the computational complexity of a query I have. The query contains multiple nested self left joins, starting with a simple select, then doing a self left join with the...
6
by: Thomas Beutin | last post by:
Hi, i've a speed problem withe the following statement: SELECT DISTINCT pz.l1_id, pz.l2_id, pz.l3_id, pz.l4_id FROM ot_adresse AS a, ot_produkt AS p LEFT OUTER JOIN ot_kat_prod AS pz ON (...
14
by: mike | last post by:
I'm using postgresl 7.3.2 and have a query that executes very slowly. There are 2 tables: Item and LogEvent. ItemID (an int4) is the primary key of Item, and is also a field in LogEvent. Some...
20
by: Julian32 | last post by:
Hello All, Sorry for the double post but I realized this topic would fit better in this forum. Before this I'd never before needed to use the "LEFT JOIN" command, and unfortunately it's giving...
4
by: polycom | last post by:
Assistance needed to optimize this query SELECT SD.content_id AS Id, SD.title AS Title, CT.name AS Contenttype, PV.content_id AS SponsorId, ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.