SQL Server左连接计数

SQL Server左连接计数,sql,sql-server,Sql,Sql Server,我得到了这个示例代码 create table #Reservation (id int identity(1,1), name varchar(50) ); create table #Reservation_details (id int identity(1,1), reservation_id int, reservation_date date, seated tinyint ); create table #Payment (id int identity(1,1),

我得到了这个示例代码

create table #Reservation
(id int identity(1,1),
 name varchar(50)
);

create table #Reservation_details
(id int identity(1,1),
 reservation_id int,
 reservation_date date,
 seated tinyint
);

create table #Payment
(id int identity(1,1),
 reservation_id int,
 payment decimal(18,2)
);

insert into #Reservation(name)
values ('Spiderman'),('Superman'),('Batman'),('Hulk');

insert into #Reservation_details(reservation_id,reservation_date,seated)
values (1,'2017-12-07',0),(2,'2017-12-08',0),(3,'2017-12-08',1),(4,'2017-12-08',0);

insert into #Payment(reservation_id,payment)
values(1,220),(2,1000)

select
 A.id,
 A.name,
 B.reservation_date,
 B.seated,
 C.payment
from #Reservation A
inner join #Reservation_details B
on B.reservation_id = A.id
left join #Payment C
on C.reservation_id = B.reservation_id
where reservation_date = '2017-12-08'

drop table #Reservation
drop table #Reservation_details
drop table #Payment
我想获得日期为“2017-12-08”且座位=1或付款不为空的所有预订的计数

我已经尝试过将日期设置为“2017-12-08”且座位=1或付款不在此处 空的

但它却告诉我过去的日期以及如何使用where as count?

试试看

select COUNT(*)
from #Reservation A
inner join #Reservation_details B on B.reservation_id = A.id
left join #Payment C on C.reservation_id = B.reservation_id
where reservation_date = '2017-12-08'
  and (B.seated=1 or C.payment is not null)
我想您忘记在
操作中使用括号
(条件1或条件2)

请参阅运算符优先级-

在您的情况下,如果不使用括号

reservation_date = '2017-12-08' and B.seated=1 or C.payment is not null
(reservation_date = '2017-12-08' and B.seated=1) or C.payment is not null
等于以下值

试试看

我想您忘记在
操作中使用括号
(条件1或条件2)

请参阅运算符优先级-

在您的情况下,如果不使用括号

reservation_date = '2017-12-08' and B.seated=1 or C.payment is not null
(reservation_date = '2017-12-08' and B.seated=1) or C.payment is not null
等于以下值

您可以使用HAVING in WHERE子句来使用COUNT函数,它应该是GROUP by子句之后的最后一个语句


您可以使用HAVING in WHERE子句来使用COUNT函数,它应该是最后一个语句,在GROUP BY子句之后

您可能只需要布尔表达式中的括号。在日期之后,sir?是否
保留
保留\u详细信息
有1-1关系?您可能只需要布尔表达式中的括号。在日期之后,sir?是否
保留
预订详情
是否有1-1关系?