从三个表中检索数据

时间:2013-12-13 15:10:18

标签: c# mysql asp.net sql

                cmd =new SqlCommand("Select i.InvoiceNo,i.TotalAmount,
                     i.PaymentStatus,m.MovieID,m.MovieName, s.CompanyName
                     From InvoiceDetails i INNER JOIN Movie m ON 
                     i.InvoiceNo=m.InvoiceNo and Supplier s 
                     Inner Join Movie m ON s.SupplierID=m.SupplierID 
                     Where SupplierID=@supplierID AND 
                     CompanyName=@companyName AND PaymentStatus=@ddlPaymentStatus",
                     conPayment);

这是错误的Select语句,如何修改它以便成功从三个表中获取数据。

1 个答案:

答案 0 :(得分:1)

我跳出来的一件事就是一次JOIN做错了:

你有:

...
from InvoiceDetails i
inner join Movie m on i.InvoiceNo = m.InvoiceNo
  and Supplier s
inner join Movie m on s.SupplierID = m.SupplierID
...

应该是:

select i.InvoiceNo,i.TotalAmount,i.PaymentStatus,m.MovieID,m.MovieName,s.CompanyName
from InvoiceDetails i
inner join Movie m on i.InvoiceNo = m.InvoiceNo
inner join Supplier s on s.SupplierID = m.SupplierID
where s.SupplierID = @supplierID
  and s.CompanyName = @companyName
  and i.PaymentStatus = @ddlPaymentStatus

您已加入Movie一次,然后您必须加入supplier。你这样做的方式肯定会给你一个语法错误。一旦有两个具有相同别名的表,然后它也会抱怨不知道供应商,因为它会期望在此之前加入的表中的列。