使用 Count 函数对数据库进行计数 SQL
Count on a database using Count function SQL
我有这样的数据库模式
Flights(flno,from,to,distance,departs,arrives,price)
Aircraft(aid,aname,cruisingRange)
Certified(employee,aircraft)
Employees(eid,ename,salary)
其中Flno为主键,每条路由对应一个"flno"。
所以我有这个关于架构的问题要回答
对于每个飞行员,列出他们的员工 ID、姓名和航线数量
他会驾驶。
我有这个 SQL,这个正确吗? (我可以测试,因为我没有数据库的数据)。
select eid, ename, count(flno)
from employees, flights
groupby flno
这是一个简单的问题,但正如每个人都提到的,您在员工和航班之间没有任何 link。关系停止在 certified
。
你显然已经或将建立某种关系。我写了一个查询,考虑到您将在员工和航班之间建立多对多关系,该查询将为您提供计数。这意味着一个员工可以有很多航班,而一个航班可以由许多员工乘坐。
航班(flno,from,to,distance,departs,arrives,price)
飞机(援助,姓名,巡航范围)
认证(员工,飞机)
员工(开斋节、姓名、薪水)
select
e.eid employee_id,
e.ename employee_name,
count(*)
from
employees e
inner join certified c on
c.employee = e.eid
inner join aircraft a on
a.aid = c.aircraft
inner join aircraft_flights af on -- new table that you would need to create
af.aircraft = a.aid and
inner join flights f on
f.flno = af.flno -- not I made up a relationship here which needs to exist in some for or another
group by
e.eid,
e.ename
我希望这至少向您展示了如何正确编写计数语句,但您可能应该复习一下对 joins.
的理解
希望对您有所帮助。
编辑
如果没有关系和在您的评论中工作,您可以获得如下计数。
select
e.eid employee_id,
e.ename employee_name,
count(*)
from
employees e
inner join certified c on
c.employee = e.eid
inner join aircraft a on
a.aid = c.aircraft
inner join flights f on
f.distance <= a.cruisingRange
group by
e.eid,
e.ename
我有这样的数据库模式
Flights(flno,from,to,distance,departs,arrives,price)
Aircraft(aid,aname,cruisingRange)
Certified(employee,aircraft)
Employees(eid,ename,salary)
其中Flno为主键,每条路由对应一个"flno"。
所以我有这个关于架构的问题要回答
对于每个飞行员,列出他们的员工 ID、姓名和航线数量 他会驾驶。
我有这个 SQL,这个正确吗? (我可以测试,因为我没有数据库的数据)。
select eid, ename, count(flno)
from employees, flights
groupby flno
这是一个简单的问题,但正如每个人都提到的,您在员工和航班之间没有任何 link。关系停止在 certified
。
你显然已经或将建立某种关系。我写了一个查询,考虑到您将在员工和航班之间建立多对多关系,该查询将为您提供计数。这意味着一个员工可以有很多航班,而一个航班可以由许多员工乘坐。
航班(flno,from,to,distance,departs,arrives,price) 飞机(援助,姓名,巡航范围) 认证(员工,飞机) 员工(开斋节、姓名、薪水)
select
e.eid employee_id,
e.ename employee_name,
count(*)
from
employees e
inner join certified c on
c.employee = e.eid
inner join aircraft a on
a.aid = c.aircraft
inner join aircraft_flights af on -- new table that you would need to create
af.aircraft = a.aid and
inner join flights f on
f.flno = af.flno -- not I made up a relationship here which needs to exist in some for or another
group by
e.eid,
e.ename
我希望这至少向您展示了如何正确编写计数语句,但您可能应该复习一下对 joins.
的理解希望对您有所帮助。
编辑
如果没有关系和在您的评论中工作,您可以获得如下计数。
select
e.eid employee_id,
e.ename employee_name,
count(*)
from
employees e
inner join certified c on
c.employee = e.eid
inner join aircraft a on
a.aid = c.aircraft
inner join flights f on
f.distance <= a.cruisingRange
group by
e.eid,
e.ename