简体   繁体   English

从两个表联接查询

[英]Joining Query from Two Tables

[Using SQL Server 2000] [使用SQL Server 2000]

I can't believe a simple task is being this complex to write in SQL. 我不敢相信,用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', '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

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). 我只需要一个简单的表,这些表的序列号放在一列中,它们在哪个表中(system_id),何时登录(日期时间)。

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. 我应该补充说,在一个表中的序列号可能会或可能不会在其他表中的一个,所以我希望显示ap.serial_Number等。

[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

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. 您正在加入serial_Number列,但您的order by语句未指定用于order by语句的表。 Anytime tables share column names you need specify which table you're referring to. 任何时候表共享列名,您都需要指定要引用的表。

如有疑问,我总是将表名放在每个字段名之前,例如“ acp_parts.serial_Number”,而不是“ serial_Number”等,这通常可以解决歧义。

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. 我想您应该在select语句和order by子句中都使用ap.serial_Number。

Hope this Helps!! 希望这可以帮助!!

You're not using the table aliases in the SELECT other than the JOIN. 除了JOIN之外,您没有在SELECT中使用表别名。 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

So, the number will only exist in one table or the other? 那么,数字将仅存在于一个表中还是另一个表中? I think you want a UNION ALL. 我想您想要一个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  

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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