【问题标题】:SQLITE INNERJOIN Nightmare, need a solution to thisSQLITE INNERJOIN 噩梦,需要一个解决方案
【发布时间】:2010-02-08 02:51:27
【问题描述】:

很高兴找到这样一个有天才成员的有用网站。我一直在尝试为这个 SQLITE 问题找到解决方案一段时间。谷歌没有帮助我,除了找到这个网站。 SQL 查询在同一数据库的 MSAccess 版本上运行良好。

这是我的 SQL 语句 - 对我不起作用。


SELECT Invoices.InvoiceNumber, Invoices.Quantity,Invoices.Code, Invoices.Price,Invoices.Discount, Invoices.InvoiceGrandTotal, Employees.EmployeeName, Customers.CustomerName, Invoices.DateOfInvoice, [price]*[Quantity] AS Total, Customers.Address, Products.Description,Products.Unit  
    FROM Products 
        INNER JOIN (
            (   
                ( Invoices INNER JOIN InvoiceDetails 
                    ON Invoices.InvoiceNumber = InvoiceDetails.InvoiceNumber
                ) INNER JOIN Customers 
                    ON Invoices.CustomerID = Customers.CustomerID
            ) INNER JOIN Employees 
                ON Invoices.UserID = Employees.EmployeeID
        ) ON Products.Code = InvoiceDetails.Code  
    WHERE (((InvoiceDetails.InvoiceNumber)='10111'));

错误信息是:Cannot compile Select-Statement: no such column: Invoices.InvoiceNumber

【问题讨论】:

  • 发布您的创建表脚本。
  • 我找到了解决方案。 Ron Savage 给出的解决方案可以无缝运行。

标签: sql sqlite


【解决方案1】:

这通常意味着您拼错了列名...检查您的 Invoices 表并确保该列是 InvoiceNumber 而不是“Invoice_Number”或类似的东西...

此外,这个查询的一个更简单的版本看起来像这样......没有所有奇怪的嵌套:

SELECT 
   Invoices.InvoiceNumber, 
   Invoices.Quantity,
   Invoices.Code, 
   Invoices.Price,
   Invoices.Discount, 
   Invoices.InvoiceGrandTotal, 
   Employees.EmployeeName, 
   Customers.CustomerName, 
   Invoices.DateOfInvoice, 
   [price]*[Quantity] AS Total, 
   Customers.Address, 
   Products.Description,
   Products.Unit 
FROM
   Invoices

   JOIN Employees 
      ON Employees.EmployeeID = Invoices.UserID 

   JOIN Customers 
      ON Customers.CustomerID = Invoices.CustomerID

   JOIN InvoiceDetails 
      ON InvoiceDetails.InvoiceNumber = Invoices.InvoiceNumber

   JOIN Products
      ON Products.Code = InvoiceDetails.Code

WHERE 
   InvoiceDetails.InvoiceNumber = '10111'

【讨论】:

  • +1 用于将垃圾 sql 清理为更具可读性的内容。
  • 感谢 Ron Savage,我欠你很多。您的 SQL 语句运行良好。
  • Shaamil,如果可行,请接受正确的答案。单击此答案上的白色复选标记,以便 Ron 获得代表,该问题的未来访问者将很快看到正确答案是什么。
【解决方案2】:

我认为问题可能在于区分大小写。除非我弄错了,否则 MS Access 字段名称不区分大小写。检查 SQLITE 表定义中违规列名的大小写是否正确。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-11-17
    • 1970-01-01
    • 2016-03-31
    • 1970-01-01
    • 1970-01-01
    • 2023-01-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多