我是SQL的初学者,我需要帮助来获得这种情况的解决方案。
对于每个景点,输出2013年访问此景点的人数。
create table visitor(
visitID char(n),
name char(n) not null,
primary key (visitID)
);
create table ticket(
ticketID char(n),
ticketType char(n),
day int(n),
month char(n),
year int(n),
seasonID char(n),
visitID char(n),
primary key (ticketID), foreign key (seasonID) references seasonPerClass, foreign
key (visitID) references visitor
);
create table attractionVisit(
attractionID char(n),
ticketID char(n),
foreign key (attractionID) references attraction, foreign key (ticketID) references
ticket
);
create table attraction(
attractionID char(n),
attractionName char(n) not null,
primary key (attractionID)
);
我无法测试我的代码,但到目前为止我已经
了SELECT AttractionName, count(distinct VisitID)
FROM (Visitation NATURAL JOIN AttractionVisit)
WHERE year = 2013;
我不知道这是否正确。请帮忙。
答案 0 :(得分:0)
您的查询中需要group by
:
SELECT AttractionName, count(distinct VisitID)
FROM (Visitation NATURAL JOIN AttractionVisit)
GROUP BY AttractionName
WHERE year = 2013;
答案 1 :(得分:0)
select att.attractionName, count(att.attractionId) as count
from visitor v inner join ticket t on ( v.visitID = t.visitID)
inner join attractionVisit av on (t.ticketID = av.ticketID)
inner join attraction att on (av.attractionID = att.attractionID)
where t.year = 2013
group by att.attractionName`
希望这是有帮助的。