MySQL案例陈述 [英] MySQL Case Statement

查看:67
本文介绍了MySQL案例陈述的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法理解如何在Select查询中使用Case语句.我想做的是将数据从1和0分别更改为是和否.IsLaptop是我的客户表中的一个字段,该字段的1和0不允许为空.以下是我的整个Select查询,案例不断在查询表达式" CASE IsLaptop中抛出语法错误(缺少运算符)",当"1"然后是"时,当"0"然后否"结束时".

I''m having trouble understanding how to use the Case Statement in a Select query. What I''m trying to do is change the data from 1s and 0s to Yes and No. IsLaptop is a field within my customers table that has 1 and 0 not allowing nulls. Below is my whole Select Query, the Case keeps throwing "Syntax error (missing operator) in query expression ''CASE IsLaptop WHEN "1" THEN "Yes" WHEN "0" THEN "No" END''.

SELECT Customers.CustomerID, Customers.OldCustomerID, LastName+','+FirstName+' '+MiddleName AS [Customer Name], Left(FirstName,1)+Left(MiddleName,1)+LastName AS UserName, zlu_Cars.Description AS Car, zlu_CarColor.Description AS [Car Color], zlu_Computers.Description AS Computer, 


CASE IsLaptop WHEN "1" THEN "Yes" WHEN "0" THEN "No" END AS Laptop,


zlu_Race.Description AS Race, zlu_Residence.Description AS Residence, zlu_BirthMonth.Description AS [Birth Month]
 
FROM (((((Customers INNER JOIN zlu_Cars ON Customers.CarID = zlu_Cars.CarID) INNER JOIN zlu_CarColor ON Customers.CarColorID = zlu_CarColor.CarColorID) INNER JOIN zlu_Computers ON Customers.ComputerID = zlu_Computers.ComputerID) INNER JOIN zlu_Race ON Customers.RaceID = zlu_Race.RaceID) INNER JOIN zlu_Residence ON Customers.ResidenceID = zlu_Residence.ResidenceID) INNER JOIN zlu_BirthMonth ON Customers.BirthMonthID = zlu_BirthMonth.BirthMonthID
WHERE (((Customers.CustomerID)>499 And (Customers.CustomerID)<601))
ORDER BY Customers.LastName, Customers.FirstName;

推荐答案

case语句似乎正确,请检查代码的其他部分.
The case statement seems to be correct, check the other sections of the code..
SELECT 
	Customers.CustomerID, 
	Customers.OldCustomerID, 
	LastName+','+FirstName+' '+MiddleName AS [Customer Name], 
	Left(FirstName,1)+Left(MiddleName,1)+LastName AS UserName, 
	zlu_Cars.Description AS Car, 
	zlu_CarColor.Description AS [Car Color], 
	zlu_Computers.Description AS Computer, 
 	CASE IsLaptop WHEN '1' THEN 'Yes' WHEN '0' THEN 'No' END AS Laptop,
 	zlu_Race.Description AS Race, 
 	zlu_Residence.Description AS Residence, 
 	zlu_BirthMonth.Description AS [Birth Month]
FROM (((((Customers INNER JOIN zlu_Cars ON Customers.CarID = zlu_Cars.CarID) INNER JOIN zlu_CarColor ON Customers.CarColorID = zlu_CarColor.CarColorID) INNER JOIN zlu_Computers ON Customers.ComputerID = zlu_Computers.ComputerID) INNER JOIN zlu_Race ON Customers.RaceID = zlu_Race.RaceID) INNER JOIN zlu_Residence ON Customers.ResidenceID = zlu_Residence.ResidenceID) INNER JOIN zlu_BirthMonth ON Customers.BirthMonthID = zlu_BirthMonth.BirthMonthID
WHERE (((Customers.CustomerID)>499 And (Customers.CustomerID)<601))
ORDER BY Customers.LastName, Customers.FirstName;

--Example..

SELECT Name, Rating,
CASE Rating
WHEN 'V' THEN 'Very Good'
WHEN 'X' THEN 'Poor'
ELSE 'Average'
END AS myratings 
FROM table_name;


这篇关于MySQL案例陈述的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆