By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,952 Members | 1,384 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,952 IT Pros & Developers. It's quick & easy.

Left Join Not Returning All Rows

P: 18
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 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 | FileAccess |
| 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 this test 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
Share this Question
Share on Google+
20 Replies


MMcCarthy
Expert Mod 10K+
P: 14,534
Try this:

sql = "SELECT file_List.FileName, IIf(file_table.UserID=2,file_table.FileAccess,"") " & _
"FROM file_List LEFT JOIN file_table " & _
" ON file_List.FileName=file_table.FileName"


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 this test it has been given the value two.)
if user id is being passed as a variable let me know the variable name and I'll change the above accordingly
Oct 29 '06 #2

NeoPa
Expert Mod 15k+
P: 31,342
You seem to have the main table second.
I don't know if this would work with a RIGHT JOIN as I never use them, but this is how I would do it.

BTW I'm assuming file_list maps to list_table in your explanation data.
Expand|Select|Wrap|Line Numbers
  1. sql = "SELECT file_list.FileName, file_table.FileAccess " & _
  2. "FROM file_table LEFT JOIN file_list " & _
  3. "ON file_table.FileAccess = file_list.FileName " & _
  4. "WHERE file_table.UserID = 1"
In your code you had the WHERE clause select records where UserID = 2 which would explain your results.
Oct 29 '06 #3

P: 18
Hello all,

Thanks to both of you for your help.

I will repost the three tables since as was pointed out by NeoPa I incorrecly named some tables.


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


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

file_list:
| ListID | FileName |
| 1 | file1.txt |
| 2 | file2.txt |

Basically I want to query these tables to have on the first column a list of the filenames (file_list.FileName), followed by another column with the same value (file_table.FileAccess) if the User has access to the file.

Try this:

sql = "SELECT file_List.FileName, IIf(file_table.UserID=2,file_table.FileAccess,"") " & _
"FROM file_List LEFT JOIN file_table " & _
" ON file_List.FileName=file_table.FileName"



if user id is being passed as a variable let me know the variable name and I'll change the above accordingly
Hello mmcarthy,

I tried this method and it came close to working except that the iif always returns NULL. I end up with this (I changed the my mistaken table and column names so that's not the cause of the error):

FileName | Expr1001
file1.txt |
file2.txt | file2.txt
file2.txt |

I tried SELECT DISTINCT to avoid file2.txt from being queried twice (don't even know why it's doing that since there's only one file2.txt in "FileName") and all it did was move file2.txt on the second column down one row.

You seem to have the main table second.
I don't know if this would work with a RIGHT JOIN as I never use them, but this is how I would do it.

BTW I'm assuming file_list maps to list_table in your explanation data.
Expand|Select|Wrap|Line Numbers
  1. sql = "SELECT file_list.FileName, file_table.FileAccess " & _
  2. "FROM file_table LEFT JOIN file_list " & _
  3. "ON file_table.FileAccess = file_list.FileName " & _
  4. "WHERE file_table.UserID = 1"
In your code you had the WHERE clause select records where UserID = 2 which would explain your results.
Hello NeoPa,

The table which I want all rows to be displayed from is file_list since I want to be able to see every file, and a corresponding column with the same file name if the user has access to it. I did try your code and surprisingly it returns the same recordset. BTW the UserId will be compared to another variable which can either have the value of 1 or 2 (to see the File Access Privileges of both user 1 and 2).

Thanks again for your help, if either of you see something I overlooked I'd appreciate any help. In the meanwhile I'll try to do this in two different quieres.

Thanks,
Julian
Oct 30 '06 #4

MMcCarthy
Expert Mod 10K+
P: 14,534
try this:

sql = "SELECT file_list.FileName, First(file_table.FileAccess) As FAccess " & _
"FROM file_table RIGHT JOIN file_List " & _
"ON file_table.FileName = file_List.FileName " & _
"WHERE file_table.UserID = 2 " & _
"GROUP BY file_list.FileName;"

BTW I am assuming UserID is a number ...
Oct 30 '06 #5

P: 18
try this:

sql = "SELECT file_list.FileName, First(file_table.FileAccess) As FAccess " & _
"FROM file_table RIGHT JOIN file_List " & _
"ON file_table.FileName = file_List.FileName " & _
"WHERE file_table.UserID = 2 " & _
"GROUP BY file_list.FileName;"

BTW I am assuming UserID is a number ...
Hello mmccarthy,

I'm still receiving the same output:

FIleName | FAccess
file2.txt | file2.txt

For some reason it's not displaying file1.txt from FileName and NULL for the corresponding FAccess column. And yes UserID is a number.

Thanks for helping,
Julian
Oct 30 '06 #6

MMcCarthy
Expert Mod 10K+
P: 14,534
try this:

Expand|Select|Wrap|Line Numbers
  1.  
  2. sql = "SELECT file_list.FileName, " & _
  3. "IIf(IsNull(file_table.FileAccess),"",file_table.FileAccess) As FAccess " & _
  4. "FROM file_table RIGHT JOIN file_List " & _
  5. "ON file_table.FileName = file_List.FileName " & _
  6. "WHERE file_table.UserID = 2 " & _
  7. "GROUP BY file_list.FileName;"
  8.  
  9.  
Oct 30 '06 #7

MMcCarthy
Expert Mod 10K+
P: 14,534
Forget the last one, try this instead ...

Expand|Select|Wrap|Line Numbers
  1.  
  2. sql = "SELECT file_list.FileName, " & _
  3. "IIf(file_table.UserID=2, file_table.FileAccess, "") As FAccess " & _
  4. "FROM file_table RIGHT JOIN file_List " & _
  5. "ON file_table.FileName = file_List.FileName " & _
  6. "GROUP BY file_list.FileName;"
  7.  
  8.  
Oct 30 '06 #8

P: 18
Forget the last one, try this instead ...

Expand|Select|Wrap|Line Numbers
  1.  
  2. sql = "SELECT file_list.FileName, " & _
  3. "IIf(file_table.UserID=2, file_table.FileAccess, "") As FAccess " & _
  4. "FROM file_table RIGHT JOIN file_List " & _
  5. "ON file_table.FileName = file_List.FileName " & _
  6. "GROUP BY file_list.FileName;"
  7.  
  8.  
Hello mmccarthy,

I tried that code but I get an error since in the last paremeter of the IIf ("") are not included since they are processed as string start and end quotes. I tried changing "" to '' and get this error:

Microsoft OLE DB Provider for ODBC Drivers error '80040e14'

[Microsoft][ODBC Microsoft Access Driver] You tried to execute a query that does not include the specified expression 'IIf(file_table.UserID=2,file_table.FileAccess,'') ' as part of an aggregate function.

Any further help will be greatly appreciated,
Julian
Oct 30 '06 #9

MMcCarthy
Expert Mod 10K+
P: 14,534
Expand|Select|Wrap|Line Numbers
  1.  
  2. sql = "SELECT file_list.FileName, " & _
  3. "IIf(file_table.UserID=2, file_table.FileAccess,Null) As FAccess " & _
  4. "FROM file_table RIGHT JOIN file_List " & _
  5. "ON file_table.FileName = file_List.FileName " & _
  6. "GROUP BY file_list.FileName;"
  7.  
  8.  
Try this ...
Oct 30 '06 #10

P: 18
Hello mmcarthy,

This query (I combined it with a previous one you gave me) is very close to working:

Expand|Select|Wrap|Line Numbers
  1. sql = "SELECT file_list.FileName, First(IIf(file_table.UserID=1, file_table.FileAccess,Null)) As FAccess " & _
  2. "FROM file_table RIGHT JOIN file_list " & _
  3. "ON file_table.FileAccess = file_list.FileName " & _
  4. "GROUP BY file_list.FileName"
When I tried it with UserID = 2 it worked and produced the desired result. However when I tried it with UserID = 1 it gave me this:

FileName | FAccess
file1.txt | file1.txt
file2.txt |

For some reason it's not displaying the second value in FAccess. Perhaps the FIRST clause is only returning the first row?

Thanks again,
Julian
Oct 30 '06 #11

MMcCarthy
Expert Mod 10K+
P: 14,534
sql = "SELECT file_list.FileName, " & _
"IIf(file_table.UserID=2, file_table.FileAccess,Null) As FAccess " & _
"FROM file_table RIGHT JOIN file_List " & _
"ON file_table.FileName = file_List.FileName " & _
"GROUP BY file_list.FileName;"


Hello mmcarthy,

This query (I combined it with a previous one you gave me) is very close to working:

Expand|Select|Wrap|Line Numbers
  1. sql = "SELECT file_list.FileName, First(IIf(file_table.UserID=1, file_table.FileAccess,Null)) As FAccess " & _
  2. "FROM file_table RIGHT JOIN file_list " & _
  3. "ON file_table.FileAccess = file_list.FileName " & _
  4. "GROUP BY file_list.FileName"
When I tried it with UserID = 2 it worked and produced the desired result. However when I tried it with UserID = 1 it gave me this:

FileName | FAccess
file1.txt | file1.txt
file2.txt |

For some reason it's not displaying the second value in FAccess. Perhaps the FIRST clause is only returning the first row?

Thanks again,
Julian
If you take off the First() statement will it work ..
Oct 30 '06 #12

P: 18
sql = "SELECT file_list.FileName, " & _
"IIf(file_table.UserID=2, file_table.FileAccess,Null) As FAccess " & _
"FROM file_table RIGHT JOIN file_List " & _
"ON file_table.FileName = file_List.FileName " & _
"GROUP BY file_list.FileName;"




If you take off the First() statement will it work ..
Hello mmccarthy,

When I take out the First () statement it gives me this error:

You tried to execute a query that does not include the specified expression as part of an aggregate function.

I believe it has something to do with the Group by, but if I take it away the first column starts to get duplicated values even with SELECT DISTINCT.

Regards,
Julian
Oct 30 '06 #13

MMcCarthy
Expert Mod 10K+
P: 14,534
try this:

sql = "SELECT file_list.FileName, " & _
"IIf(file_table.UserID=2, file_table.FileAccess,Null) As FAccess " & _
"FROM file_table RIGHT JOIN file_List " & _
"ON file_table.FileName = file_List.FileName " & _
"GROUP BY file_list.FileName, IIf(file_table.UserID=2, file_table.FileAccess,Null);"
Oct 30 '06 #14

P: 18
try this:

sql = "SELECT file_list.FileName, " & _
"IIf(file_table.UserID=2, file_table.FileAccess,Null) As FAccess " & _
"FROM file_table RIGHT JOIN file_List " & _
"ON file_table.FileName = file_List.FileName " & _
"GROUP BY file_list.FileName, IIf(file_table.UserID=2, file_table.FileAccess,Null);"
I receive this error:

Syntax error (missing operator) in query expression 'IIf(file_table.UserID=1, file_table.FileAccess,Null) As FAccess'.

Regards,
Julian
Oct 30 '06 #15

MMcCarthy
Expert Mod 10K+
P: 14,534
I receive this error:

Syntax error (missing operator) in query expression 'IIf(file_table.UserID=1, file_table.FileAccess,Null) As FAccess'.

Regards,
Julian
did you include the ' As FAccess' in the group by statement?

If you did remove it.

If not post the query as you're using it now.
Oct 30 '06 #16

P: 18
did you include the ' As FAccess' in the group by statement?

If you did remove it.

If not post the query as you're using it now.
No I did not remove it, here's the current query:

Expand|Select|Wrap|Line Numbers
  1. sql = "SELECT file_list.FileName, IIf(file_table.UserID=1, file_table.FileAccess,Null) As FAccess " & _
  2. "FROM file_table RIGHT JOIN file_list " & _
  3. "ON file_table.FileAccess = file_list.FileName " & _
  4. "GROUP BY file_list.FileName, IIf(file_table.UserID=1, file_table.FileAccess,Null) As FAccess "
  5.  
I'm thinking of changing FileAccess to a foreign key, then quering FileAccess based on UserID, saving query to an array, quering FileName, and checking to see if an item in the array matches.

Inneficient I know, but at least I know it will work in the meantime.

Regards,
Julian
Oct 31 '06 #17

MMcCarthy
Expert Mod 10K+
P: 14,534
No I did not remove it, here's the current query:

Expand|Select|Wrap|Line Numbers
  1. sql = "SELECT file_list.FileName, IIf(file_table.UserID=1, file_table.FileAccess,Null) As FAccess " & _
  2. "FROM file_table RIGHT JOIN file_list " & _
  3. "ON file_table.FileAccess = file_list.FileName " & _
  4. "GROUP BY file_list.FileName, IIf(file_table.UserID=1, file_table.FileAccess,Null) As FAccess "
  5.  
REMOVE IT...

Paste in query exactly as below:


sql = "SELECT file_list.FileName, IIf(file_table.UserID=1, file_table.FileAccess,Null) As FAccess " & _
"FROM file_table RIGHT JOIN file_list " & _
"ON file_table.FileAccess = file_list.FileName " & _
"GROUP BY file_list.FileName, IIf(file_table.UserID=1, file_table.FileAccess,Null);"
Oct 31 '06 #18

P: 18
REMOVE IT...

Paste in query exactly as below:


sql = "SELECT file_list.FileName, IIf(file_table.UserID=1, file_table.FileAccess,Null) As FAccess " & _
"FROM file_table RIGHT JOIN file_list " & _
"ON file_table.FileAccess = file_list.FileName " & _
"GROUP BY file_list.FileName, IIf(file_table.UserID=1, file_table.FileAccess,Null);"
Hello mccarthy,

It goes back to what it was doing before the GROUP BY statement, it has repeats in the FileName column. Any way to filter that?
Oct 31 '06 #19

P: 18
Thanks to everyone who helped. I fixed the problem by making file_table.FileAccess a foreign key to ListID, and playing around with the ON statement. I thought of swithcing the IIf from the SELECT to the ON and Eureka!!

Here's the final statement:

Expand|Select|Wrap|Line Numbers
  1. sql = "SELECT file_list.FileName, file_table.listID FROM file_list LEFT " & _ 
  2. "JOIN file_table ON file_list.ListID = " & _
  3. "IIf(file_table.UserID=1,file_table.ListID,Null)"
I was this close to giving up and choosing to use an inneficient array.

Thanks again,
Julian
Oct 31 '06 #20

MMcCarthy
Expert Mod 10K+
P: 14,534
Thanks to everyone who helped. I fixed the problem by making file_table.FileAccess a foreign key to ListID, and playing around with the ON statement. I thought of swithcing the IIf from the SELECT to the ON and Eureka!!

Here's the final statement:

Expand|Select|Wrap|Line Numbers
  1. sql = "SELECT file_list.FileName, file_table.listID FROM file_list LEFT " & _ 
  2. "JOIN file_table ON file_list.ListID = " & _
  3. "IIf(file_table.UserID=1,file_table.ListID,Null)"
I was this close to giving up and choosing to use an inneficient array.

Thanks again,
Julian
You're welcom Julian. It's true that most of these problems arise because the table structure is not correct. I should probably have pointed that out.
Oct 31 '06 #21

Post your reply

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