我从来没有这样做,而且只是为了在asp.net中显示gridview控件的信息。
这就是我所拥有的:
SELECT [VisitNumber], [Date], [DateFreq], [Weight], [Fat], [BloodPressure], [Pulse], [Total], [Discount], [NetTotal], [AmountPaid], [PaymentStatus], [TypeOfPayment], [Comments] FROM [tblVisits] WHERE ([PatientID] = '16')
SELECT [DatePrescribed], [AdditionalSupply], [Medication], [Dose], [Quantity], [Type], [Description], [Expires], [Lot] FROM [tblMedications] WHERE (([PatientID] = '16') AND ([DatePrescribed] = (select top 1 dateprescribed from tblMedications where ([PatientID] = '16') order by DatePrescribed desc))) ORDER BY [DatePrescribed] DESC
有没有办法将它们组合起来,也许是在PatientID上完全加入?我对sql不是很了解,所以任何想法都会受到赞赏。
答案 0 :(得分:0)
似乎您希望通过patientID
进行某种SQL连接。
inner join
。null
的所有记录,其中ID在一个表中而不在另一个表中,请使用full outer join
。null
,请使用left
或right outer join
。看起来你很可能想要inner join
,如下所示:
select v.VisitNumber, v.Date, ..., v.Comments, m.DatePrescribed, m.AdditionalSupply...
from tblVisits as v inner join tblMedication as m
on v.PatientID = m.PatientID;
答案 1 :(得分:0)
根据您的评论,您正在尝试根据访问当天开具的药物加入药物治疗。在这种情况下,您需要加入该信息:
SELECT [tblVisits].[VisitNumber], [tblVisits].[Date], ..., [tblVisits].[Comments],
[tblMedications].[DatePrescribed], ..., [tblMedications].[Expires], [tblMedications].[Lot]
FROM [tblVisits]
JOIN [tblMedications] ON [tblVisits].[PatientID] = [tblMedications].[PatientID]
AND [tblVisits].[Date] = [tblMedications].[DatePrescribed]
WHERE [tblVisits].[PatientID] = '16'
ORDER BY [tblVisits].[Date]
我想提一下,我认为说这两个表是“无关的”并不是最好的方式。根据日期,它们确实为您的应用程序建立了有意义的关系;它只是以外键的形式不明确,因为它不是基于密钥的关系。