佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

查看: 2569|回复: 16

求query language

[复制链接]
发表于 26-3-2015 03:35 PM | 显示全部楼层 |阅读模式
求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 算出来?

query.jpg
query2.jpg
query1.jpg
query3.jpg
回复

使用道具 举报


ADVERTISEMENT

发表于 26-3-2015 05:46 PM | 显示全部楼层
本帖最后由 nick_khor 于 26-3-2015 05:48 PM 编辑

我是 sql 新人,不知道帮不帮得到你
  1. select svc.carregistrationno from service svc
  2.         left outer join car on car.carregno=svc.carregistrationno
  3. where count(svc.carname) >= 2 and car.carname = 'Ford'
复制代码


回复

使用道具 举报

 楼主| 发表于 26-3-2015 05:51 PM | 显示全部楼层
nick_khor 发表于 26-3-2015 05:46 PM
我是 sql 新人,不知道帮不帮得到你

不能。。这题我问了那么多人都没人会。。
是不是题目不清楚还是题目出错了?
回复

使用道具 举报

发表于 26-3-2015 06:26 PM | 显示全部楼层
cry8383 发表于 26-3-2015 05:51 PM
不能。。这题我问了那么多人都没人会。。
是不是题目不清楚还是题目出错了?

我觉得这个方法有点不 efficient,但我试了试行得通的
  1. select * from (
  2. select svc.carregistrationno, count(car.carname) as "count" from service svc
  3.         left outer join car on car.carregno=svc.carregistrationno
  4. where car.carname = 'Ford'
  5. group by svc.carregistrationno ) temp where temp.count > 1
复制代码


回复

使用道具 举报

发表于 26-3-2015 07:26 PM | 显示全部楼层

  1. select *
  2. from service t0
  3. left join car t1 on t1.carregno = t0.carregistrationno
  4. where t1.carname = 'Ford'
  5. having count(*) >= 2
复制代码
回复

使用道具 举报

 楼主| 发表于 26-3-2015 07:48 PM | 显示全部楼层
nick_khor 发表于 26-3-2015 06:26 PM
我觉得这个方法有点不 efficient,但我试了试行得通的

可是他要的是福特车 维修过2次以上。。

SELECT customerno AS payment, *
FROM car INNER JOIN service ON service.CarRegistrationNo = car.CarRegNo
WHERE CarRegNo = "6893";


我用了这个。。可是我知道没有用count是错的。。
回复

使用道具 举报

Follow Us
 楼主| 发表于 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是福特来过两次维修。。
回复

使用道具 举报


ADVERTISEMENT

发表于 26-3-2015 08:10 PM | 显示全部楼层
cry8383 发表于 26-3-2015 07:52 PM
这个我也试了。。也是不行。。
你看。。现在我的car table只有carregno 和 service 的carregisterationn ...
  1. select t1.CarRegNofrom Service t0
  2. left join Car t1 on t1.CarRegNo = t0.CarRegistrationNo
  3. where t1.CarName = 'Ford'
  4. having count(t1.CarRegNo) >= 2;
复制代码


回复

使用道具 举报

发表于 26-3-2015 08:14 PM | 显示全部楼层
这个比较准确,拿掉 Where 和 Having 就可以看到全部

  1. select t1.CarName, t1.CarRegNo, count(t1.CarRegNo) as ServiceCount
  2. from Service t0
  3. left join Car t1 on t1.CarRegNo = t0.CarRegistrationNo
  4. where t1.CarName = 'Ford'
  5. GROUP BY t1.CarName, t1.CarRegNo
  6. HAVING COUNT(t1.CarRegNo) >= 2;
复制代码
回复

使用道具 举报

 楼主| 发表于 26-3-2015 08:21 PM | 显示全部楼层
hooi1983 发表于 26-3-2015 08:14 PM
这个比较准确,拿掉 Where 和 Having 就可以看到全部


可是不可以显示福特的 carregno, cartype, carcolor, yearofregisteration, carname?

他的问题是interested in knowing details of Ford cars
回复

使用道具 举报

发表于 26-3-2015 08:26 PM | 显示全部楼层
cry8383 发表于 26-3-2015 08:21 PM
可是不可以显示福特的 carregno, cartype, carcolor, yearofregisteration, carname?

他的问题是int ...

你可不可以自己加在 Select & Group By 那边
回复

使用道具 举报

 楼主| 发表于 26-3-2015 08:26 PM | 显示全部楼层
hooi1983 发表于 26-3-2015 08:14 PM
这个比较准确,拿掉 Where 和 Having 就可以看到全部

我知道怎么做了。。谢谢你hooi1983..

回复

使用道具 举报

发表于 26-3-2015 09:49 PM | 显示全部楼层
本帖最后由 musicalangel 于 26-3-2015 09:50 PM 编辑

虽然LZ已经得到答案了, 这边我提供另外以一种答案, 但是不用having

首先, 先统计Service table 里面的所有个别车辆来的次数


  1. select CarRegistrationNo, count(*) as num from SERVICE group by CarRegistrationNo
复制代码


只要你的CarRegistrationNo 是有编索引(他本来就是FK, 一般上都会编Index)的话, 上面的query 就不会有速度上的问题

OK, 现在你有了每台车的Service 次数, 你把他跟 CAR 的 table 进行join


  1. select CarRegNo, YearOfRegristration, CarName, CarType, CarColor, T1.num from CAR inner join
  2.    (select CarRegistrationNo, count(*) as num from SERVICE group by CarRegistrationNo) as T1
  3. on T1.CarRegistrationNo=CAR.CarRegNo
复制代码


这时就显示了所有车来过的次数(num)

但是我还是不知道那辆阿?

最后只要加上 where 就完成了


  1. select CAR.CarRegNo, CAR.YearOfRegristration, CAR.CarName, CAR.CarType, CAR.CarColor, T1.num from CAR inner join
  2.    (select CarRegistrationNo, count(*) as num from SERVICE group by CarRegistrationNo) as T1
  3. on T1.CarRegistrationNo=CAR.CarRegNo
  4. where CAR.CarName="Ford" and T1.Num>=2
复制代码


这样就完成了.

为什么不用having?
请看看这个:
having vs where

共勉之.

评分

参与人数 1人气 +5 收起 理由
moot + 5 谢谢分享

查看全部评分

回复

使用道具 举报

 楼主| 发表于 26-3-2015 10:39 PM | 显示全部楼层
musicalangel 发表于 26-3-2015 09:49 PM
虽然LZ已经得到答案了, 这边我提供另外以一种答案, 但是不用having

首先, 先统计Service table 里面的所 ...

musicalangel 谢谢你。。你解释的太详细。。
谢谢!!
回复

使用道具 举报

发表于 27-3-2015 09:13 AM | 显示全部楼层
musicalangel 发表于 26-3-2015 09:49 PM
虽然LZ已经得到答案了, 这边我提供另外以一种答案, 但是不用having

首先, 先统计Service table 里面的所 ...

wokay! 谢谢
回复

使用道具 举报


ADVERTISEMENT

发表于 30-3-2015 12:01 AM | 显示全部楼层
musicalangel 发表于 26-3-2015 09:49 PM
虽然LZ已经得到答案了, 这边我提供另外以一种答案, 但是不用having

首先, 先统计Service table 里面的所 ...

小弟很佩服大大,不过你拿 Having vs Where 就不怎么正确,应该拿 Having vs Sub Query 才对

评分

参与人数 1人气 +1 收起 理由
musicalangel + 1 谢谢指教

查看全部评分

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

 

ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


版权所有 © 1996-2023 Cari Internet Sdn Bhd (483575-W)|IPSERVERONE 提供云主机|广告刊登|关于我们|私隐权|免控|投诉|联络|脸书|佳礼资讯网

GMT+8, 27-4-2024 07:06 AM , Processed in 0.076397 second(s), 32 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表