简体   繁体   English

SQL CASE WHEN问题,查询根本不运行

[英]SQL CASE WHEN issue, Query not running at all

I have a small database for a retail scenario, within this I have a field called "Dispatched" which is a bool to indicate if the item has been dispatched yet. 我有一个用于零售业务的小型数据库,在该数据库中,我有一个名为“ Dispatched”的字段,该字段用于指示商品是否已经发货。 This is of course as 1 and 0, I have attempted a simple CASE WHEN to make 1 display as Yes and 0 as No. 当然,这是1和0,我尝试了一种简单的情况,当1显示为Yes,0显示为No。

My full query is: 我的完整查询是:

SELECT 
orders.OrdersID, 
stock.ItemName, 
basket.Quantity, 
customer.FirstName, 
customer.LastName, 
address.AddressLine1, 
address.AddressLine2, 
address.TownOrCity, 
address.Postcode, 
address.Country, 
CASE WHEN basket.Dispatched = 1 THEN 'Yes' ELSE 'No' END AS basket.Dispatched 
FROM orders
JOIN OrdersBasketJoin ON orders.OrdersID = OrdersBasketJoin.OrdersID
LEFT JOIN basket ON OrdersBasketJoin.BasketID = basket.BasketID
JOIN customer ON orders.CustomerID = customer.CustomerID
JOIN address ON orders.DeliveryAddress = address.AddressID
JOIN stock ON basket.StockID = stock.StockID
ORDER BY  `customer`.`CustomerID` ASC
LIMIT 0 , 30

The query works fine without the CASE WHEN, and will display the 1s and 0s when Dispatched is selected normally, as well as WHERE working fine when referencing Dispatched. 该查询可以在没有CASE WHEN的情况下正常运行,并且在正常选择Dispatched时将显示1和0,而在引用Dispatched时WHERE可以正常工作。

However when I try adding 但是当我尝试添加

CASE WHEN basket.Dispatched = 1 THEN 'Yes' ELSE 'No' END AS basket.Dispatched

I get the error 我得到错误

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '.Dispatched FROM orders JOIN OrdersBasketJoin ON orders.OrdersID = Ord' at line 12

From what I've researched this is pretty much as simple of a CASE WHEN you can do, and the syntax is correct I believe. 根据我的研究,这几乎就像一个案例一样简单,并且我相信语法是正确的。

Not sure if its just a visual bug, but "END" in the CASE doesn't light up like it is known to be a function, whereas JOIN, ON, LEFT etc all light up, no matter where END doesn't. 不知道它是否只是一个视觉错误,但是CASE中的“ END”不会像已知的功能那样亮起,而JOIN,ON,LEFT等都亮起,无论END不在哪里。

Any and all help is much appreciated -Tom 任何和所有帮助都非常感激-汤姆

You are missing with , after address.Country therefore you are getting syntax error try this one 您在address.Country之后缺少,因此您遇到语法错误尝试此操作

SELECT 
orders.OrdersID, 
stock.ItemName, 
basket.Quantity, 
customer.FirstName, 
customer.LastName, 
address.AddressLine1, 
address.AddressLine2, 
address.TownOrCity, 
address.Postcode, 
address.Country, 
(CASE WHEN basket.Dispatched = 1 THEN 'Yes' ELSE 'No' END) AS `basket.Dispatched` 
FROM orders
JOIN OrdersBasketJoin ON orders.OrdersID = OrdersBasketJoin.OrdersID
LEFT JOIN basket ON OrdersBasketJoin.BasketID = basket.BasketID
JOIN customer ON orders.CustomerID = customer.CustomerID
JOIN address ON orders.DeliveryAddress = address.AddressID
JOIN stock ON basket.StockID = stock.StockID
ORDER BY  `customer`.`CustomerID` ASC
LIMIT 0 , 30

放反引号:

AS `basket.Dispatched` 
CASE WHEN basket.Dispatched = 1 THEN 'Yes' ELSE 'No' END AS 'basket.Dispatched'

You are missing single quotes before and after the aliase name. 您在别名名称前后缺少单引号。

I have tried this in my DEV machine without the single quote and have the same error. 我在没有单引号的DEV机器上尝试了此操作,并且遇到相同的错误。

Msg 102, Level 15, State 1, Line 1
Incorrect syntax near '.'.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM