查看: 2569|回复: 16
|
求query language
[复制链接]
|
|
求query "ford" 车维修2次以上。。
The Garage centre is interested in knowing details of Ford cars which have come for more than two times for servicing. Design a query to display the requested details.
怎么用count 算出来?
|
|
|
|
|
|
|
|
发表于 26-3-2015 05:46 PM
|
显示全部楼层
本帖最后由 nick_khor 于 26-3-2015 05:48 PM 编辑
我是 sql 新人,不知道帮不帮得到你
- select svc.carregistrationno from service svc
- left outer join car on car.carregno=svc.carregistrationno
- where count(svc.carname) >= 2 and car.carname = 'Ford'
复制代码
|
|
|
|
|
|
|
|
楼主 |
发表于 26-3-2015 05:51 PM
|
显示全部楼层
不能。。这题我问了那么多人都没人会。。
是不是题目不清楚还是题目出错了?
|
|
|
|
|
|
|
|
发表于 26-3-2015 06:26 PM
|
显示全部楼层
我觉得这个方法有点不 efficient,但我试了试行得通的
- select * from (
- select svc.carregistrationno, count(car.carname) as "count" from service svc
- left outer join car on car.carregno=svc.carregistrationno
- where car.carname = 'Ford'
- group by svc.carregistrationno ) temp where temp.count > 1
复制代码
|
|
|
|
|
|
|
|
发表于 26-3-2015 07:26 PM
|
显示全部楼层
- select *
- from service t0
- left join car t1 on t1.carregno = t0.carregistrationno
- where t1.carname = 'Ford'
- having count(*) >= 2
复制代码 |
|
|
|
|
|
|
|
楼主 |
发表于 26-3-2015 07:48 PM
|
显示全部楼层
可是他要的是福特车 维修过2次以上。。
SELECT customerno AS payment, *
FROM car INNER JOIN service ON service.CarRegistrationNo = car.CarRegNo
WHERE CarRegNo = "6893";
我用了这个。。可是我知道没有用count是错的。。
|
|
|
|
|
|
|
|
楼主 |
发表于 26-3-2015 07:52 PM
|
显示全部楼层
这个我也试了。。也是不行。。
你看。。现在我的car table只有carregno 和 service 的carregisterationno 是一样。。
而我的car table的carname 只有一个table。。
那我要找的是福特维修过两次。。
只能才service 里面找carregisterationno 是来过两次的。。
福特车牌是6893来过两次。。
|
|
|
|
|
|
|
|
楼主 |
发表于 26-3-2015 07:56 PM
|
显示全部楼层
而且我的显示的是福特车的details,来过两次维修。。
然后呢。。我的service 的carregisterationo 6893是福特来过两次维修。。 |
|
|
|
|
|
|
|
发表于 26-3-2015 08:10 PM
|
显示全部楼层
- select t1.CarRegNofrom Service t0
- left join Car t1 on t1.CarRegNo = t0.CarRegistrationNo
- where t1.CarName = 'Ford'
- having count(t1.CarRegNo) >= 2;
复制代码
|
|
|
|
|
|
|
|
发表于 26-3-2015 08:14 PM
|
显示全部楼层
这个比较准确,拿掉 Where 和 Having 就可以看到全部
- select t1.CarName, t1.CarRegNo, count(t1.CarRegNo) as ServiceCount
- from Service t0
- left join Car t1 on t1.CarRegNo = t0.CarRegistrationNo
- where t1.CarName = 'Ford'
- GROUP BY t1.CarName, t1.CarRegNo
- HAVING COUNT(t1.CarRegNo) >= 2;
复制代码 |
|
|
|
|
|
|
|
楼主 |
发表于 26-3-2015 08:21 PM
|
显示全部楼层
可是不可以显示福特的 carregno, cartype, carcolor, yearofregisteration, carname?
他的问题是interested in knowing details of Ford cars
|
|
|
|
|
|
|
|
发表于 26-3-2015 08:26 PM
|
显示全部楼层
你可不可以自己加在 Select & Group By 那边
|
|
|
|
|
|
|
|
楼主 |
发表于 26-3-2015 08:26 PM
|
显示全部楼层
|
|
|
|
|
|
|
发表于 26-3-2015 09:49 PM
|
显示全部楼层
本帖最后由 musicalangel 于 26-3-2015 09:50 PM 编辑
虽然LZ已经得到答案了, 这边我提供另外以一种答案, 但是不用having
首先, 先统计Service table 里面的所有个别车辆来的次数
- select CarRegistrationNo, count(*) as num from SERVICE group by CarRegistrationNo
复制代码
只要你的CarRegistrationNo 是有编索引(他本来就是FK, 一般上都会编Index)的话, 上面的query 就不会有速度上的问题
OK, 现在你有了每台车的Service 次数, 你把他跟 CAR 的 table 进行join
- select CarRegNo, YearOfRegristration, CarName, CarType, CarColor, T1.num from CAR inner join
- (select CarRegistrationNo, count(*) as num from SERVICE group by CarRegistrationNo) as T1
- on T1.CarRegistrationNo=CAR.CarRegNo
复制代码
这时就显示了所有车来过的次数(num)
但是我还是不知道那辆阿?
最后只要加上 where 就完成了
- select CAR.CarRegNo, CAR.YearOfRegristration, CAR.CarName, CAR.CarType, CAR.CarColor, T1.num from CAR inner join
- (select CarRegistrationNo, count(*) as num from SERVICE group by CarRegistrationNo) as T1
- on T1.CarRegistrationNo=CAR.CarRegNo
- where CAR.CarName="Ford" and T1.Num>=2
复制代码
这样就完成了.
为什么不用having?
请看看这个:
having vs where
共勉之.
|
评分
-
查看全部评分
|
|
|
|
|
|
|
楼主 |
发表于 26-3-2015 10:39 PM
|
显示全部楼层
|
|
|
|
|
|
|
发表于 27-3-2015 09:13 AM
|
显示全部楼层
wokay! 谢谢
|
|
|
|
|
|
|
|
发表于 30-3-2015 12:01 AM
|
显示全部楼层
小弟很佩服大大,不过你拿 Having vs Where 就不怎么正确,应该拿 Having vs Sub Query 才对 |
评分
-
查看全部评分
|
|
|
|
|
|
| |
本周最热论坛帖子
|