用于预订的SQL Server

时间:2015-03-13 00:38:34

标签: sql performance

嗨,在我设法进行查询的人的帮助下,但我发现必须使用我不完全了解的连接!我想知道一个月内预订的门票数量!是下面的代码没关系或我需要更多的研究任何帮助将不胜感激

SELECT * FROM (选择bookingDate,perID,count(预订)作为一个月内预订的门票数量 来自门票 GROUP BY perID)T INNER JOIN预订b on t.bookingID = b.bookingID

我错过了什么吗?

数据和3表如下:

create table bookings( bookingID int IDENTITY (1,1) primary key not null, customerID int Foreign Key references customers, clerkID varchar (20) Foreign Key references clerks, bookingDate Date,
bookingTime time, bookingType varchar(20), totalCost decimal(5,2), );

create table performances( perID int Not Null IDENTITY (1,1) primary key, perName varchar (50), perTime Time, perDate Date, perType varchar (20), duration varchar (10), screen int, ageLimit varchar (10), location varchar (10),
);

create table tickets ( ticketNo int Not Null IDENTITY (1,1) primary key, bookingID int foreign key references bookings, perID int foreign key references performances, ticketType varchar(10), ticketDate date, ticketPrice decimal (4,2), screen int, perTime time, );

``

1 个答案:

答案 0 :(得分:0)

试试这个:

SELECT
*
FROM (
         SELECT perID, count(*) as ticket_count
         FROM tickets
         GROUP BY perID
    ) T
INNER JOIN  performances p on t.perID = p.perID  
;

当我不知道列要求是什么时,使用“select *”。始终指定列并在执行此操作时包括表别名。

e.g。

SELECT
p.perName, p.perDate, p.perType, t.ticket_count
FROM (
         SELECT perID, count(*) as ticket_count
         FROM tickets
         GROUP BY perID
    ) T
INNER JOIN  performances p on t.perID = p.perID  
;