472,954 Members | 1,607 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,954 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 2076
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: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
1
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.