简体   繁体   English

使用CASE更改SELECT返回值

[英]Changing SELECT return values with CASE

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

Please read the MySQL manual on the case statement : 请阅读MySQL手册中的case语句

case object_room.object_room_type_id
        when 1 then (1 * object_room.quantity)
        when 2 then (2 * object_room.quantity)
        when 3 then (3 * object_room.quantity)
        when 4 then (5 * object_room.quantity)
        when 5 then (1 * object_room.quantity)
        when 6 then (4 * object_room.quantity)
        when 8 then (2 * object_room.quantity)
        when 9 then (3 * object_room.quantity)
end

Either you have a table object_room_type or you should create one. 您有一个表object_room_type或应该创建一个。 I have assumed that you call the room capacity column capacity . 我假设您将“房间容量”列称为“ capacity Then you can join the object_room_type table in your query: 然后,您可以在查询中加入object_room_type表:

SELECT 
object.id, object.name, 
location.address, 
location.postcode, 
location_city.`name`,
object_room_type.capacity * object_room.quantity
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
LEFT JOIN object_room_type ON object_room_type.object_room_type_id = object_room.object_room_type_id

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

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