我有一个选择查询,返回一个计数值

时间:2019-10-16 14:13:37

标签: sql oracle

请使用更新命令帮助我

select guest_no,count(flag_booking) 
from booking 
where flag_booking = 'C' 
group by guest_no;

现在我想使用更新命令来更新它,即更​​新将分别包含每个不同来宾的flag_booking计数的字段

update guest 
set complete_booking = (
    select count(*) 
    from booking 
    join guest on guest.guest_no = booking.guest_no 
    group by guest.guest_no,guest.flag_booking 
    having guest.flag_booking = 'C'
) ;

这不起作用。

3 个答案:

答案 0 :(得分:1)

您需要关联子查询:

<div class="a">
  <p><a [attr.href]="url" target="_blank">{{"dont'know number" | translate}}</a></p>
</div>

在此查询中,子查询仅返回一个记录,并且具有唯一列,该列包含有关来宾的预订计数。因此,您不需要update guestg g set complete_booking = ( select count(*) from booking b where g.guest_no = b.guest_no and b.flag_booking = 'C' ) ; ,也不需要group by

答案 1 :(得分:1)

使用相关子查询:

update guest g
  set complete_booking = (select count(*) 
                          from booking b
                          where b.guest_no = g.guest_no and
                                b.flag_booking = 'C'
                         ) ;

joingroup by不会执行您想要的操作。首先,group by可能会返回多行,这将导致查询的运行时失败。其次,子查询与外部查询无关,因此所有行都将获得相同的值。

答案 2 :(得分:0)

您不需要在子查询中加入,也不需要分组依据:

update guest 
  set complete_booking = (select count(*) 
                          from booking 
                          where guest.guest_no = booking.guest_no) 
                            and booking.flag_booking = 'C');
相关问题