Joining Query from Two Tables
[Using SQL Server 2000]
I can't believe a simple task is being this complex to write in SQL.
Perhaps I'm missing something.
Here's the query that will not run because the column names are ambiguous:
SELECT serial_Number, system_id, date_time
FROM acp_parts ap
INNER JOIN join test_results tr ON (ap.serial_Number=tr.serial_Number)
WHERE serial_Number IN (
'C037687 1000 11', 'C037687 1001 11', 'C037687 1002 11', 'C037687 1003 11',
'C037687 1004 11', 'C037687 1005 11', 'C037687 1006 11', 'C037687 1007 11',
'C037687 1008 11', 'C037687 1009 11', 'C037687 1010 11', 'C037687 1011 11',
'C037687 1012 11', 'C037687 1013 11', 'C037687 1014 11', 'C037687 1015 11',
'C037687 1016 11', 'C开发者_C百科037687 1017 11', 'C037687 1018 11', 'C037687 1019 11',
'C037687 1020 11', 'C037687 1021 11', 'C037687 1022 11', 'C037687 1023 11',
'C037687 1024 11')
ORDER BY serial_Number, date_time
I'd just like a simple table with these serial numbers in one column, what table they are in (system_id), and when they were logged (date_time).
I do NOT want a separate column for each table - that would defeat the purpose of my query.
This is probably something simple that I'm missing.
[EDIT]: Sorry! I should have added that serial numbers in one table may or may not be in one of the other tables, so I do not want to display ap.serial_Number, etc.
[Solution]: Here's what I went with that solved my problem:
SELECT serial_Number, system_id, date_time FROM (
select serial_Number, system_id, date_time
from acp_parts ap
UNION ALL
select serial_Number, system_id, date_time
from test_results tr
) T
where serial_Number in (
'C037687 1000 11', 'C037687 1001 11', 'C037687 1002 11', 'C037687 1003 11', 'C037687 1004 11', 'C037687 1005 11',
'C037687 1006 11', 'C037687 1007 11', 'C037687 1008 11', 'C037687 1009 11', 'C037687 1010 11', 'C037687 1011 11',
'C037687 1012 11', 'C037687 1013 11', 'C037687 1014 11', 'C037687 1015 11', 'C037687 1016 11', 'C037687 1017 11',
'C037687 1018 11', 'C037687 1019 11', 'C037687 1020 11', 'C037687 1021 11', 'C037687 1022 11', 'C037687 1023 11',
'C037687 1024 11')
order by serial_Number, date_time
So, the number will only exist in one table or the other? I think you want a UNION ALL. The following is not syntax checked, but should get you started:
SELECT * FROM
(
select
serial_Number,
system_id,
date_time
from
acp_parts ap
UNION ALL
select
serial_Number,
system_id,
date_time
from
acp_parts test_results tr
)
ORDER BY serial_Number, date_time
You are joining on the column serial_Number
, but your order by statement isn't specifying which table to use for the order by statement. Anytime tables share column names you need specify which table you're referring to.
When in doubt, I always put table name before each field name like 'acp_parts.serial_Number' instead of 'serial_Number' etc, that usually resolves ambiguity.
You surely have the same column names on both tables. You need to alias the tables and specify which column from which table are you trying to select. Example:
select ap.serial_Number, tr.system_id, ap.date_time
from acp_parts ap inner join test_results tr on (ap.serial_Number=tr.serial_Number)
where serial_Number in (
'C037687 1000 11', 'C037687 1001 11', 'C037687 1002 11', 'C037687 1003 11', 'C037687 1004 11', 'C037687 1005 11',
'C037687 1006 11', 'C037687 1007 11', 'C037687 1008 11', 'C037687 1009 11', 'C037687 1010 11', 'C037687 1011 11',
'C037687 1012 11', 'C037687 1013 11', 'C037687 1014 11', 'C037687 1015 11', 'C037687 1016 11', 'C037687 1017 11',
'C037687 1018 11', 'C037687 1019 11', 'C037687 1020 11', 'C037687 1021 11', 'C037687 1022 11', 'C037687 1023 11',
'C037687 1024 11')
order by ap.serial_Number, tr.date_time
I guess you should be using ap.serial_Number in both the select statement and the order by clause.
Hope this Helps!!
You're not using the table aliases in the SELECT other than the JOIN. Assuming I have the tables correct:
select
ap.serial_Number
,tr.system_id
,tr.date_time
from acp_parts AS ap
inner join test_results AS tr on ap.serial_Number=tr.serial_Number
where ap.serial_Number in (
'C037687 1000 11', 'C037687 1001 11', 'C037687 1002 11', 'C037687 1003 11', 'C037687 1004 11', 'C037687 1005 11',
'C037687 1006 11', 'C037687 1007 11', 'C037687 1008 11', 'C037687 1009 11', 'C037687 1010 11', 'C037687 1011 11',
'C037687 1012 11', 'C037687 1013 11', 'C037687 1014 11', 'C037687 1015 11', 'C037687 1016 11', 'C037687 1017 11',
'C037687 1018 11', 'C037687 1019 11', 'C037687 1020 11', 'C037687 1021 11', 'C037687 1022 11', 'C037687 1023 11',
'C037687 1024 11')
order by
ap.serial_Number
,tr.date_time
Yes, you are missing something simple: serial_Number is in both tables, so it always has to be qualified, even though the result won't necessarily matter:
SELECT ap.serial_Number, system_id, date_time
FROM acp_parts ap
INNER JOIN join test_results tr ON (ap.serial_Number = tr.serial_Number)
WHERE ap.serial_Number IN (
'C037687 1000 11', 'C037687 1001 11', 'C037687 1002 11', 'C037687 1003 11',
'C037687 1004 11', 'C037687 1005 11', 'C037687 1006 11', 'C037687 1007 11',
'C037687 1008 11', 'C037687 1009 11', 'C037687 1010 11', 'C037687 1011 11',
'C037687 1012 11', 'C037687 1013 11', 'C037687 1014 11', 'C037687 1015 11',
'C037687 1016 11', 'C037687 1017 11', 'C037687 1018 11', 'C037687 1019 11',
'C037687 1020 11', 'C037687 1021 11', 'C037687 1022 11', 'C037687 1023 11',
'C037687 1024 11')
ORDER BY ap.serial_Number, date_time
精彩评论