在下面的代码中我有一个
文件表
document name | document id | client id
和附件表
attach id | document id | attachment name.
我想从附件表中获取attachemt名称。在下面的代码中,我从文档名称获取文档ID,文档名称,clientid,我想从附件table获取附件名称。在同一查询中。
@i_DocumentID varchar(MAX)
SELECT
DocumentID,
DocumentName,
ClientID
FROM Documents
WHERE DocumentID IN (SELECT * FROM dbo.CSVToTable(@i_DocumentID))
答案 0 :(得分:1)
@i_DocumentID varchar(MAX)
SELECT
a.DocumentID,
a.DocumentName,
a.ClientID,
b.AttachmentName
from Documents a
join Attachments b on a.DocumentId = b.DocumentId
where a.DocumentID IN (SELECT * FROM dbo.CSVToTable(@i_DocumentID))
您可以在此处详细了解联接:http://www.w3schools.com/sql/sql_join.asp或此处http://www.mssqltips.com/sqlservertip/1667/sql-server-join-example/
答案 1 :(得分:0)
使用联接:
@i_DocumentID varchar(MAX)
SELECT D.DocumentID, D.DocumentName,D.ClientID, A.Attachement_name
from Documents D JOIN
Attachment A on D.document_id=A.document_id
where D.DocumentID IN (SELECT * FROM dbo.CSVToTable(@i_DocumentID))
答案 2 :(得分:0)
你需要加入。
@i_DocumentID varchar(MAX)
SELECT d.DocumentID, DocumentName,ClientID,AttachmentName
from Documents d
INNER JOIN attachment a
ON a.document_id=d.document_id
where DocumentID IN (SELECT * FROM dbo.CSVToTable(@i_DocumentID))