我不知道如何格式化我的 FROM 语句以避免语法错误字符串文字

I don't know how to format my FROM statement to avoid syntax error string literal

提问人:Michael Turner 提问时间:10/31/2023 最后编辑:Michael Turner 更新时间:11/1/2023 访问量:45

问:

我不知道为什么SQL无法识别语句中的表名。它返回一个语法错误,指出预期的字符串文字“peak-bit-402819.warehouse_orders。订单“在 [7:10] 我试图在 FROM 语句中加入双引号,但无济于事。编辑:我添加了我使用的两个表格的细节图片。我还尝试使用详细的表名,并得到在美国找不到的相同错误。请看图片。FROM

SELECT
    Warehouse.warehouse_id,
    CONCAT(Warehouse.state, ":", Warehouse.warehouse_alias) AS warehouse_name,
    COUNT(Orders.order_id) AS number_of_orders,
    (SELECT COUNT(*) 
     FROM 'peak-bit-402819.warehouse_orders.Orders' Orders) AS total_orders,
    CASE 
        WHEN COUNT(Orders.order_id)/(SELECT COUNT(*) FROM warehouse_orders.Orders Orders) <= 0.20
            THEN "fulfilled 0-20% of Orders"
        WHEN COUNT(Orders.order_id)/(SELECT COUNT(*) FROM warehouse_orders.Orders Orders) > 0.20
             AND COUNT(Orders.order_id)/(SELECT COUNT(*) FROM warehouse_orders.Orders Orders) <= 0.60
            THEN "Fulfilled 21-60% of Orders"
        ELSE "Fulfilled more than 60% of Orders"
    END AS fulfillment_summary
FROM  
    'peak-bit-402819.warehouse_orders.Warehouse' Warehouse
LEFT JOIN 
    warehouse_orders.Orders Orders ON Orders.warehouse_id = Warehouse.warehouse_id
GROUP BY
    Warehouse.warehouse_id,
    warehouse_name
HAVING
    COUNT(Orders.order_id) > 0

使用具有短表名的 FROM 语句

仓库表名称

订单表名称

使用带有反引号和完整表名的 FROM 语句

SQL 字符串 语法

评论

0赞 Unlucky 10/31/2023
尝试使用反刻号 ('): 'peak-bit-402819.warehouse_orders'.'Warehouse' 或 'peak-bit-402819'.'warehouse_orders。Warehouse“,具体取决于您的数据库名称是否为peak-bit-402819.warehouse_orderspeak-bit-402819
0赞 Michael Turner 11/1/2023
请看新图片。我尝试用反引号编辑完整的表名,显示相同的错误。我从两个表和两张图片中添加了表名的图片,其中有刻度和没有刻度的相同错误。
0赞 Dai 11/1/2023
使用(方括号)或(双引号) - 而不是单引号 - 还要注意 没有用 brackes/引号括起来。[peak-bit-402819.warehouse_orders].[Warehouse]"peak-bit-402819.warehouse_orders"."Warehouse".

答:

0赞 Jumbalaya Bill 11/1/2023 #1

如果这些是实际的表名,您可以尝试使用括号来代替:

(SELECT COUNT(*) 
     FROM [peak-bit-402819.warehouse_orders.Orders] Orders) AS total_orders,

FROM  
    [peak-bit-402819.warehouse_orders.Warehouse] Warehouse