I have two tables, one (Organisations
) with 60 rows of data and the other (Question Data
) which has hundreds of rows of data. They both have a column OrgID
which is a text field of 5 characters length.
The SQL statement is:
SELECT Organisations.Co, Organisations.Type, Organisations.F_O, Question_Data.* FROM Organisations INNER JOIN Question_Data ON Organisations.OrgID = Question_Data.OrgID;
When I run this query there are NO data returning from the Organisations
table even though I know there are multiple data matches (i.e the first three columns of the returned data are empty). Changing the join yields the same results either in reverse or (in the case of INNER JOIN
) no data at all is returned.
I imported the Organisations
table from another Access database set up on this computer (i.e same version of Access and no changes).
The one thing I’m suspicious about is that in one table, all text fields are left-justified and in the other they’re all right justified.
Any help would be appreciated. Many thanks in advance.
Seamus
Advertisement
Answer
This is your query:
SELECT o.Co, o.Type, o.F_O, qd.* FROM Organisations as o INNER JOIN Question_Data as qd ON o.OrgID = qd.OrgID;
Your JOIN
does not work — so ids that look the same are not the same. This is a problem when you use strings for ids (it cannot happen with integers).
The most common reason would be spaces at the beginning or end of the ids. You can try:
SELECT o.Co, o.Type, o.F_O, qd.* FROM Organisations as o INNER JOIN Question_Data as qd ON TRIM(o.OrgID) = TRIM(qd.OrgID);
If this is the problem, then you should fix the data so your version of the JOIN
(the better version) works.
If this doesn’t solve the problem, you might need to delve deeper. Next, you might try LIKE
to see if substring match:
SELECT o.Co, o.Type, o.F_O, qd.* FROM Organisations as o INNER JOIN Question_Data as qd ON TRIM(o.OrgID) LIKE '*' & TRIM(qd.OrgID) & '*' OR TRIM(qd.OrgID) LIKE '*' & TRIM(o.OrgID) & '*' ;
For instance, the space characters might not be simple spaces but something else — or even something invisible.