Изменение возвращаемых значений SELECT с помощью CASE - PullRequest
0 голосов
/ 02 ноября 2011

Кто-нибудь может мне помочь с этим утверждением? Я пытаюсь получить количество бус в объекте с вместимостью комнаты * количество

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

Ответы [ 2 ]

1 голос
/ 02 ноября 2011

Пожалуйста, прочитайте руководство 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
0 голосов
/ 02 ноября 2011

Либо у вас есть таблица object_room_type, либо вы должны ее создать.Я предположил, что вы называете колонку вместимости комнаты capacity.Затем вы можете присоединиться к таблице 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
...