简体   繁体   中英

Two tables, join not returning data from one table

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 (ie 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 (ie 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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM