Solved How to show right name in the UNION ALL query? (1 Viewer)

yunhsuan

Member
Local time
Today, 20:09
Joined
Sep 10, 2021
Messages
54
Hello~
I created two tables, containing No, Day, ProjectName and Site. The contents of ProjectName come from another table, called "Project".
Then, I combined this two tables by fucntion "UNION ALL" in query.
Code:
SELECT Table_1.[Day], Table_1.[ProjectName], Table_1.[Site], Table_1.[No]
FROM Table_1
UNION ALL
SELECT Table_2.[Day], Table_2.[ProjectName], Table_2.[Site], Table_2.[No]
FROM Table_2
ORDER BY Day;
But, the result shows No but not ProjectName in ProjectName field. How can it show ProjectName instead of No in the result of query?
Here is my test file: https://reurl.cc/px5WZx
Thanks in advance!
 

CJ_London

Super Moderator
Staff member
Local time
Today, 13:09
Joined
Feb 19, 2013
Messages
16,610
can't download - I'm told the site is unsafe. Please attach your file here

note that Day and No are reserved words and should not be used for field names. If you do, you can get unexplained errors which may be the case here
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 05:09
Joined
Aug 30, 2003
Messages
36,125
Each of your SELECT statements would join to the project table, along the lines of

SELECT Table_1.[Day], Projects.[ProjectName], Table_1.[Site], Table_1.[No]
FROM Table_1 INNER JOIN Projects ON Table_1.ProjectName = Projects.ProjectName

I made a guess about the field name that links them.
 

Gasman

Enthusiastic Amateur
Local time
Today, 13:09
Joined
Sep 21, 2011
Messages
14,288
Is projectname a table lookup?
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 08:09
Joined
Feb 19, 2002
Messages
43,266
This is just one of the many reasons that experts do not use table level lookups. Their implementation is spotty at best and since they are merely a crutch, you don't need to use them. You can just do things the old-fashioned way using joins as Paul suggested.

Use combos on forms or even reports, if you want but NEVER on tables.
 

plog

Banishment Pending
Local time
Today, 07:09
Joined
May 11, 2011
Messages
11,646
I created two tables, containing No, Day, ProjectName and Site

That's a sign of poor table structure. Are those the only fields in both tables? If not, what are the other fields?

What's the difference between Table1 and Table2?
 

yunhsuan

Member
Local time
Today, 20:09
Joined
Sep 10, 2021
Messages
54
can't download - I'm told the site is unsafe. Please attach your file here

note that Day and No are reserved words and should not be used for field names. If you do, you can get unexplained errors which may be the case here
Sorry...I do not know I can directly upload file here. This is the file.
 

Attachments

  • test.accdb
    412 KB · Views: 311
Last edited:

yunhsuan

Member
Local time
Today, 20:09
Joined
Sep 10, 2021
Messages
54
Each of your SELECT statements would join to the project table, along the lines of

SELECT Table_1.[Day], Projects.[ProjectName], Table_1.[Site], Table_1.[No]
FROM Table_1 INNER JOIN Projects ON Table_1.ProjectName = Projects.ProjectName

I made a guess about the field name that links them.
I tried this, the result showed error.
Code:
SELECT Table_1.Day, Table_1.ProjectName, Table_1.Site, Table_1.[No]
FROM Project INNER JOIN Table_1 ON Project.ProjectName = Table_1.ProjectName
UNION ALL
SELECT Table_2.Day, Table_2.ProjectName, Table_2.Site, Table_2.[No]
FROM Project INNER JOIN Table_2 ON Project.ProjectName = Table_2.ProjectName;
Then, I tried this:
Code:
SELECT Table_1.Day, Table_1.ProjectName, Table_1.Site, Table_1.[No]
FROM Project INNER JOIN Table_1 ON Project.[No] = Table_1.ProjectName
UNION ALL
SELECT Table_2.Day, Table_2.ProjectName, Table_2.Site, Table_2.[No]
FROM Project INNER JOIN Table_2 ON Project.[No] = Table_2.ProjectName;
The ProjectName field still show No.
How can I edit this error?
 

yunhsuan

Member
Local time
Today, 20:09
Joined
Sep 10, 2021
Messages
54
This is just one of the many reasons that experts do not use table level lookups. Their implementation is spotty at best and since they are merely a crutch, you don't need to use them. You can just do things the old-fashioned way using joins as Paul suggested.

Use combos on forms or even reports, if you want but NEVER on tables.
Excuse me~
I'm a newbie. I don't understand "old-fashioned way" in the answer very well.
Is it possible to give me more precise explanation?
 

yunhsuan

Member
Local time
Today, 20:09
Joined
Sep 10, 2021
Messages
54
Each of your SELECT statements would join to the project table, along the lines of

SELECT Table_1.[Day], Projects.[ProjectName], Table_1.[Site], Table_1.[No]
FROM Table_1 INNER JOIN Projects ON Table_1.ProjectName = Projects.ProjectName

I made a guess about the field name that links them.
I think I succeed by this:
Code:
Project.ProjectName AS ProjectName
 

CJ_London

Super Moderator
Staff member
Local time
Today, 13:09
Joined
Feb 19, 2013
Messages
16,610
your uploaded file only contains table_1 and that table does not contain the fields 'projectname' or 'no'. But I see you have marked the thread as solved so I guess it doesn't matter
 

Gasman

Enthusiastic Amateur
Local time
Today, 13:09
Joined
Sep 21, 2011
Messages
14,288
I think I succeed by this:
Code:
Project.ProjectName AS ProjectName
No, not really, as it was already called ProjectName in the first place? :)
Your problem was likely due to your table lookup in table1, where you only really hold the key for the record in ProjectName table. The lookup just shows you the linked name and hides it true value. This is why most people do not use them, as there are other issues with them as well. Search on here for lookup fields.
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 05:09
Joined
Aug 30, 2003
Messages
36,125
I tried this, the result showed error.
Code:
SELECT Table_1.Day, Table_1.ProjectName, Table_1.Site, Table_1.[No]
FROM Project INNER JOIN Table_1 ON Project.ProjectName = Table_1.ProjectName
UNION ALL
SELECT Table_2.Day, Table_2.ProjectName, Table_2.Site, Table_2.[No]
FROM Project INNER JOIN Table_2 ON Project.ProjectName = Table_2.ProjectName;
Then, I tried this:
Code:
SELECT Table_1.Day, Table_1.ProjectName, Table_1.Site, Table_1.[No]
FROM Project INNER JOIN Table_1 ON Project.[No] = Table_1.ProjectName
UNION ALL
SELECT Table_2.Day, Table_2.ProjectName, Table_2.Site, Table_2.[No]
FROM Project INNER JOIN Table_2 ON Project.[No] = Table_2.ProjectName;
The ProjectName field still show No.
How can I edit this error?

In your working SQL that still shows the ID:

SELECT Table_1.Day, Table_1.ProjectName, Table_1.Site, Table_1.[No]
FROM Project INNER JOIN Table_1 ON Project.[No] = Table_1.ProjectName

Table_1.ProjectName should be Project.ProjectName, or whatever the field name is in the project table that you want displayed.
 

Users who are viewing this thread

Top Bottom