2011-11-02 58 views
0

任何人都可以帮助我解决这个问题吗?我试图让珠量物体间的房量*量使用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 
+0

帮你做什么?对我来说看起来很好...请包括你所遇到的错误或问题的描述。 –

+1

'CASE'需要一个'WHEN'在条件... –

+0

嗨。感谢您的回复。我更正了语法(WHEN)并添加了错误信息 – PsychoX

回答

1

请阅读MySQL manual on the case statement

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

我的错误...谢谢你 – PsychoX

0

要么你有表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