SQL如何根据另一个表中满足的条件显示列值

问题描述 投票:0回答:2

您有桌椅,它描述了飞机上的座位。它有以下列:

  • seat_no - 座位的唯一编号;
  • 状态 - 的状态 座位(0表示空闲,1表示保留,2表示 已购买);
  • person_id - 预订/购买者的ID 该座位(如果对应状态为0则为0)。

您还有表请求,其中包含以下列:

  • request_id - 请求的唯一ID;
  • request - 请求的描述(1表示保留,2表示购买);
  • seat_no - 该人想要预订/购买的座位号;
  • person_id - 想要预订/购买此座位的人的 ID。

一个人可以预订/购买免费座位,也可以购买他们已预订的座位。

任务是在执行给定的请求后归还餐桌座位。

注意:请求是从最低的request_id开始申请的;保证表请求中所有的 Seat_no 值都出现在表席位中。

对于给定的桌子座位

seat_no |status|person_id
-------------------------  
1       | 1    |  1   

2       | 1    |  2    

3       | 0    |  0 

4       | 2    |  3

5       | 0    |  0

和请求表

request_id |  request | seat_no | person_id
-------------------------------------------
1          |  1       |  3      |  4   
2          |  2       |  2      |  5    
3          |  2       |  1      |  1

在请求表中再添加一行,其中我们对三号座位有多个座位请求。在这种情况下,我们必须采用请求 id 最小值的行。 4 | 2 | 3 | 1

所需的输出

seat_no | status | person_id
----------------------------
1       |  2     |  1    
2       |  1     |  2    
3       |  1     |  4    
4       |  2     |  3    
5       |  0     |  0

第一个请求已完成,因为 3 号座位有空。第二个请求被忽略,因为 2 号座位已被其他人预订。第三个请求已完成,因为1号座位已被此人预订,因此他们可以购买。

我实现解决方案的方法是将两个表席位和请求进行外部联接,然后使用条件来显示所需的结果,但此查询仅正确返回最后两行:

  select seat_no,status,person_id from

(SELECT COALESCE(r.request_id,0) as request_id,s.seat_no, 
case when s.person_id=r.person_id then r.request 
 when s.person_id=0 then  COALESCE(r.request,s.status)
 else s.status 
end as status,

case when s.person_id=0 and r.person_id is not null then r.person_id 
    when s.person_id!=r.person_id then s.person_id
  else s.person_id
end as person_id
from seats s 
left outer join requests r on s.seat_no=r.seat_no ) t 
group by request_id,t.seat_no,status,person_id 
order by seat_no asc
mysql sql
2个回答
1
投票
SELECT s.seat_no, 
  If(s.person_id = 0 || s.person_id = r.person_id, coalesce(r.request, s.status), coalesce(s.status, r.request)) as status, 
 If(s.person_id = 0 || s.person_id = r.person_id, coalesce(r.person_id, s.person_id), coalesce(s.person_id, r.person_id)) as person_id from 
seats s left join (SELECT a.*
FROM requests a
INNER JOIN 
  (SELECT seat_no,
    MIN(request_id) as request_id
  FROM requests 
  GROUP BY seat_no
) AS b
  ON a.seat_no = b.seat_no
  AND a.request_id = b.request_id)
as r on s.seat_no = r.seat_no order by s.seat_no;

0
投票
CREATE PROCEDURE solution()
BEGIN
 
with cte as (   
select request_id,
        request,
        seat_no,
        person_id,
        Rank() over (partition by seat_no order by request_id) as rank1
from requests 
        ),

 cte1 as (
    select *
    from cte 
    where rank1 = 1
)

select s.seat_no,
       case 
        when s.status = 0 and r.request = 1 then r.request
        when s.status = 1 and r.request = 2 and s.person_id = r.person_id then r.request
        when s.status = 0 and r.request = 2 then r.request
        else coalesce(s.status,0) end as status,
        case 
        when s.status = 0 and r.request = 1 then r.person_id
        when s.status = 1 and r.request = 2 and s.person_id = r.person_id then r.person_id
        when s.status = 0 and r.request = 2 then r.person_id
        else coalesce(s.person_id,0) end as person_id
from seats as s
left join cte1 as r
on s.seat_no = r.seat_no;

END
        
© www.soinside.com 2019 - 2024. All rights reserved.