473,395 Members | 1,869 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,395 software developers and data experts.

Left Join Not Returning All Rows

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
20 4859
MMcCarthy
14,534 Expert Mod 8TB
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
32,556 Expert Mod 16PB
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
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
14,534 Expert Mod 8TB
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
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
14,534 Expert Mod 8TB
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
14,534 Expert Mod 8TB
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
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
14,534 Expert Mod 8TB
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
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
14,534 Expert Mod 8TB
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
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
14,534 Expert Mod 8TB
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
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
14,534 Expert Mod 8TB
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
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
14,534 Expert Mod 8TB
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
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
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
14,534 Expert Mod 8TB
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

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 (...
4
by: Andrei Ivanov | last post by:
Hello, I have 2 tables: CREATE TABLE products ( id SERIAL PRIMARY KEY, name VARCHAR(255) NOT NULL ); CREATE TABLE products_daily_compacted_views ( product ...
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...
7
by: Julian32 | last post by:
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...
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: 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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.