我有4个表,detail_table,adders_table,form_table和summary_table 前3个表有一个共同的列piece_ID,但是第4个表summary_table没有Piece_ID列,而是有ID列,它只出现在详细信息表中而不是地址和表格表中 现在有什么方法可以将所有这4张桌子加在一起吗?
(我已成功加入表格,地址和详细信息表仅保留为摘要) 下面是3个表的查询,我得到了成功的结果。
Select archive_address.PieceID, archive_adf_detail.Job_ID, prod_code,
seq_number,process_date, seq_queue, addline1, addline2,addline3,addline4,addline5, archive_freeform.free_form1, archive_Free_Form2
from archive_Address join archive_adf_detail
on archive_address.PieceID = archive_adf_detail.F_key
join archive_freeform
on archive_address.PieceID = archive_freeform.PieceID
Where (archive_Address.addline1 like '%Cons%'
or archive_Address.addline2 like '%Cons%' )
and archive_Address.pieceID like '%FB2K%'
order by addline1
我正在使用以下查询添加第4个表格
Select archive_address.PieceID, archive_adf_detail.Job_ID, prod_code, job_summary.Status_Date,
seq_number,process_date, seq_queue, addline1, addline2,addline3,addline4,addline5, archive_freeform.free_form1, archive_freeform.Free_Form2
from archive_Address join archive_adf_detail
on archive_address.PieceID = archive_adf_detail.F_key
join archive_freeform
on archive_address.PieceID = archive_freeform.PieceID
join job_summary
on archive_adf_detail.job_ID = job_summary.job_ID
Where (archive_Address.addline1 like '%Cons%'
or archive_Address.addline2 like '%Cons%' )
and archive_Address.pieceID like '%FB2K%'
order by addline1
执行上述操作后,我收到以下错误:
Msg 207, Level 16, State 1, Line 8
Invalid column name 'jobID'.
Msg 209, Level 16, State 1, Line 1
Ambiguous column name 'prod_code'.
Msg 209, Level 16, State 1, Line 2
Ambiguous column name 'seq_number'.
Msg 209, Level 16, State 1, Line 2
Ambiguous column name 'process_date'.
Msg 207, Level 16, State 1, Line 2
Invalid column name 'archive_Free_Form2'.
如果我使用的语法不正确或者无法像这样加入第4个表格,请纠正我 谢谢!
答案 0 :(得分:0)
根据查询表达的关系连接表。你的联盟很好。
该错误与以下事实有关:列的名称在表的集合中不是唯一的(不明确的:具有多个含义)。您需要指定具有列名的表。
Select
archive_address.PieceID,
archive_Address.addline1,
archive_Address.addline2,
archive_Address.addline3,
archive_Address.addline4,
archive_Address.addline5,
archive_adf_detail.prod_code,
archive_adf_detail.seq_number,
archive_adf_detail.process_date,
archive_adf_detail.seq_queue,
archive_freeform.Job_ID,
archive_freeform.free_form1,
archive_freeform.Free_Form2,
job_summary.Status_Date
From archive_Address
Join archive_adf_detail on archive_address.PieceID = archive_adf_detail.F_key
Join archive_freeform on archive_address.PieceID = archive_freeform.PieceID
Join job_summary on archive_freeform.job_ID = job_summary.job_ID
Where (archive_Address.addline1 like '%Cons%' or archive_Address.addline2 like '%Cons%' )
and archive_Address.pieceID like '%FB2K%'
Order by archive_Address.addline1
我并不完全确定您已传达了架构,因为您问题中的错误消息与第二个查询不对应。 您需要了解哪些数据在哪里以及您想要哪些部分。