使用 CASE 更改 SELECT 返回值

发布于 2024-12-13 13:32:14 字数 1317 浏览 2 评论 0原文

任何人都可以帮我解决这个问题吗?我试图获取具有房间容量 * 数量的对象中的珠子数量

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不喜欢何必死缠烂打 2024-12-20 13:32:14

请阅读 有关 case 的 MySQL 手册声明

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

Please read the 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

您要么拥有一张object_room_type 表,要么您应该创建一个表。我假设您将房间容量列称为“容量”。然后您可以在查询中加入 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

Either you have a table object_room_type or you should create one. I have assumed that you call the room capacity column capacity. Then you can join the object_room_type table in your query:

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文