我试图对b.bookingID和ba.bookingID使用内部连接语句,但它不起作用,并且说别名'b‘不存在。
select c.customerid, customerfname,customerlname, count(b.bookingid) as
'No. of Bookings', flightdate, arrivaltime, departtime,
b.price, status, taxespaid, ba.bagpayment, p.PaymentAmount,
pt.paymentcode
from booking b, flight f, baggage ba, payment p, paymenttype pt, customer
c
where b.bookingID=ba.bookingid
and pt.paymentcode=p.paymentcode
and f.flightnum=b.flightnum
and c.customerid=b.customerID
;预期结果将显示与每个特定预订相关的所有信息。因此,它将显示数据库中的76个预订,以及每次预订的计数,而不是返回1行,其中显示count(b.bookingID)为3,000。
发布于 2019-04-29 20:33:22
如果你想知道所有不同的bookingid的数量,那么你需要计数(不同的bookingid)
select c.customerid, customerfname,customerlname
, count(distinct b.bookingid) as
'No. of Bookings', flightdate, arrivaltime
, departtime,
b.price, status, taxespaid, ba.bagpayment
, p.PaymentAmount,
pt.paymentcode
from booking b, flight f, baggage ba, payment p, paymenttype pt, customer c
where b.bookingID=ba.bookingid
and pt.paymentcode=p.paymentcode
and f.flightnum=b.flightnum
and c.customerid=b.customerID
;count()根据您的条件返回非空行的数量
所以count(*)返回所有行都不为空,
count(bookingid)返回bookingid列为not null的所有行
最后,count(distinct bookingid)返回bookingid的不同值的数字,在本例中,确定bookingid是唯一的,即预订的编号
无论如何,在没有正确分组的情况下使用聚合函数对于未参与聚合函数工作的列(默认情况下)仅适用于mysql版本低于5.7的mysql版本从mysql 5.7开始的mysql版本不允许此行为,如果group by,则应添加不在聚合函数中的列
https://stackoverflow.com/questions/55903435
复制相似问题