使用 CASE 更改 SELECT 返回值
任何人都可以帮我解决这个问题吗?我试图获取具有房间容量 * 数量的对象中的珠子数量
SELECT
object.id, object.name,
location.address,
location.postcode,
location_city.`name`,
(
select
case
when object_room.object_room_type_id = 1 then (1 * object_room.quantity)
when object_room.object_room_type_id = 2 then (2 * object_room.quantity)
when object_room.object_room_type_id = 3 then (3 * object_room.quantity)
when object_room.object_room_type_id = 4 then (5 * object_room.quantity)
when object_room.object_room_type_id = 5 then (1 * object_room.quantity)
when object_room.object_room_type_id = 6 then (4 * object_room.quantity)
when object_room.object_room_type_id = 8 then (2 * object_room.quantity)
when object_room.object_room_type_id = 9 then (3 * object_room.quantity)
end CASE
from object_room
)
FROM object
LEFT JOIN location ON object.location_id = location.id
LEFT JOIN location_city ON location.location_city_id = location_city.id
LEFT JOIN object_room ON object.id = object_room.object_id
我收到此错误:
[Err] 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 'CASE
from object_room
Anyone can help me with this statement? I'm trying to get amount of beads in object with room capacity * quantity
SELECT
object.id, object.name,
location.address,
location.postcode,
location_city.`name`,
(
select
case
when object_room.object_room_type_id = 1 then (1 * object_room.quantity)
when object_room.object_room_type_id = 2 then (2 * object_room.quantity)
when object_room.object_room_type_id = 3 then (3 * object_room.quantity)
when object_room.object_room_type_id = 4 then (5 * object_room.quantity)
when object_room.object_room_type_id = 5 then (1 * object_room.quantity)
when object_room.object_room_type_id = 6 then (4 * object_room.quantity)
when object_room.object_room_type_id = 8 then (2 * object_room.quantity)
when object_room.object_room_type_id = 9 then (3 * object_room.quantity)
end CASE
from object_room
)
FROM object
LEFT JOIN location ON object.location_id = location.id
LEFT JOIN location_city ON location.location_city_id = location_city.id
LEFT JOIN object_room ON object.id = object_room.object_id
I got this error:
[Err] 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 'CASE
from object_room
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请阅读 有关
case 的 MySQL 手册
声明:Please read the MySQL manual on the
case
statement:您要么拥有一张
object_room_type
表,要么您应该创建一个表。我假设您将房间容量列称为“容量”。然后您可以在查询中加入object_room_type
表:Either you have a table
object_room_type
or you should create one. I have assumed that you call the room capacity columncapacity
. Then you can join theobject_room_type
table in your query: