Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql SQL如何根据另一个表中满足的条件显示列值_Mysql_Sql - Fatal编程技术网

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

Mysql SQL如何根据另一个表中满足的条件显示列值,mysql,sql,Mysql,Sql,你有桌子座位,它描述了飞机上的座位。它有以下列: 座椅编号-座椅的唯一编号 status(状态)-服务器的状态 座位(0表示空闲,1表示保留,2表示保留 购买) person_id-预订/购买人员的id 此座椅(如果相应的状态为0,则为0) 您还有表请求,其中包含以下列: 请求\u id-请求的唯一id 请求-请求的描述(1表示保留,2表示购买) seat_no-该人员想要预订/购买的座位号 person_id-想要预订/购买此座位的人的id 一个人可以预订/购买免费座位,也可以购买他们

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

  • 座椅编号-座椅的唯一编号
  • status(状态)-服务器的状态 座位(0表示空闲,1表示保留,2表示保留 购买)
  • person_id-预订/购买人员的id 此座椅(如果相应的状态为0,则为0)
您还有表请求,其中包含以下列:

  • 请求\u id-请求的唯一id
  • 请求-请求的描述(1表示保留,2表示购买)
  • seat_no-该人员想要预订/购买的座位号
  • person_id-想要预订/购买此座位的人的id
一个人可以预订/购买免费座位,也可以购买他们预订的座位

任务是在执行给定请求后返回表席位

注意:从最低请求id应用请求;可以保证表seats中的SEATE_no的所有值都显示在表seats中

对于给定的桌子和座位

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
在requests表中再添加一行,其中我们对3号座位有多个座位请求。在这种情况下,我们必须使用请求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

你的查询给出了你想要的结果,那么问题出在哪里呢?它没有给出你想要的结果。仅匹配所需结果的两行确定对我来说可以得到什么结果我只正确获得了最后两行。如果为前三个获取不同的值,则休息。另外,我怀疑我的方法是否正确。如果有更好和优化的方法来实现解决方案,如果你没有得到与小提琴相同的结果,那么你没有使用相同的代码,或者发布的数据不是代表性的样本。请确认小提琴产生了预期的效果
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;