我在PHP中针对Access数据库编写SQL查询,并且我收到此错误:
" SQLSTATE [42000]:语法错误或访问冲突:-3100 [Microsoft] [ODBC Microsoft Access驱动程序]查询表达式中的语法错误(缺少运算符)'(FROM [Tbl_Invoice_Details],INNER JOIN [Tbl_Product_Table] ON [Tbl_Invoice_Details.Product_ID] = [Tbl_Product_Table.Product_ID])WHERE Invoice_ID = Pa_RaM000'。"
这是我的问题:
SELECT [Tbl_Invoice_Details.Product_ID],
[Tbl_Invoice_Details.Seed Size], [Tbl_Invoice_Details.Ref2], [Tbl_Invoice_Details.RefNo], [Tbl_Invoice_Details.Quantity Ordered],
[Tbl_Invoice_Details.Quantity Delivered], [Tbl_Invoice_Details.PricePerUnit], [Tbl_Invoice_Details.Extended],
[Tbl_Product_Table.Product_ID], [Tbl_Product_Table.Hybrid Brand], (FROM [Tbl_Invoice_Details] INNER JOIN [Tbl_Product_Table]
ON [Tbl_Invoice_Details.Product_ID] = [Tbl_Product_Table.Product_ID]) WHERE Invoice_ID = ?
为什么会失败?
答案 0 :(得分:2)
您的查询失败,因为方括号中的所有内容都被解释为单个名称,因此
SELECT [Tbl_Invoice_Details.Product_ID] ... FROM [Tbl_Invoice_Details]
与
相同SELECT [Tbl_Invoice_Details].[Tbl_Invoice_Details.Product_ID] ... FROM [Tbl_Invoice_Details]
Access不允许我们定义包含句点的字段名称,因此永远不会起作用。
你的意思是
SELECT [Tbl_Invoice_Details].[Product_ID] ... FROM [Tbl_Invoice_Details]
是(正确的)一种表达方式
SELECT [TableName].[FieldName] ... FROM [TableName]
答案 1 :(得分:1)
以下查询如何:
SELECT i.Product_ID, i.[Seed Size], i.Ref2, i.RefNo, i.[Quantity Ordered],
i.[Quantity Delivered], i.PricePerUnit, i.Extended, p.[Hybrid Brand]
FROM Tbl_Invoice_Details i
INNER JOIN Tbl_Product_Table p ON i.Product_ID = p.Product_ID
WHERE i.Invoice_ID = ?
答案 2 :(得分:0)
确实,不需要[Tbl_Product_Table.Hybrid_Brand]之后的逗号..
也许你的发票在锚点之间?
所以
WHERE Invoice_ID = Pa_RaM000
变成了
WHERE Invoice_ID = "Pa_RaM000"