求:一道关于SQL的面试题?

hckxzy 2012-03-26 11:52:59
一面试题以oracle为准:
Order表与OrderItem之间是一对多的关系:
Order表
id name
1 1name
2 2name
OrderItem表
id orderId name
1 1 Iphone
2 1 Iphone
3 2 Iphone
4 2 Iphone
用一条SQL:
如何统计出含有Iphone的订单数量,
不能用这种方式:
select count(*) from (
select distinct orderId from OrderItem where name = 'Iphone';
),希望一条SQL查出。
...全文
259 24 打赏 收藏 转发到动态 举报
写回复
用AI写文章
24 条回复
切换为时间正序
请发表友善的回复…
发表回复
成一粒 2012-03-26
  • 打赏
  • 举报
回复
select count(*) from OrderItem where name='Iphone'
hckxzy 2012-03-26
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 flagiris 的回复:]

7L的不行么???

引用 14 楼 hckxzy 的回复:

请大侠继续发帖解题。。。
[/Quote]
可以,谢谢
hckxzy 2012-03-26
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 flagiris 的回复:]

当然跟简单的也可以写成这样

SQL code

select count(distinct orderId) from OrderItem where name = 'Iphone'
[/Quote]
可以实现,谢谢。。。
hckxzy 2012-03-26
  • 打赏
  • 举报
回复
[Quote=引用 21 楼 liujia229 的回复:]

SQL code
select count(distinct orderid) from orderitem where name = 'iphone'
[/Quote]
给力
悲剧的人参 2012-03-26
  • 打赏
  • 举报
回复
select count(distinct orderid) from orderitem where name = 'iphone'
beiouwolf 2012-03-26
  • 打赏
  • 举报
回复
Order表
id name
1 1name
2 2name
OrderItem表
id orderId name
1 1 Iphone
2 1 Iphone
3 2 Iphone
4 2 Iphone

select count(*) from order o inner join orderItem item on o.id=item.orderid where item.name='iphone' group by o.name
cxw3152 2012-03-26
  • 打赏
  • 举报
回复
select SUM(*) from OrderItem group by orderId where name = 'Iphone';
TommyWu01 2012-03-26
  • 打赏
  • 举报
回复
select count(*) from OrderItem group by orderId where name = 'Iphone';
菖蒲老先生 2012-03-26
  • 打赏
  • 举报
回复
select count(distinct orderId) from OrderItem where name = 'Iphone'
菖蒲老先生 2012-03-26
  • 打赏
  • 举报
回复
7L的不行么???

[Quote=引用 14 楼 hckxzy 的回复:]

请大侠继续发帖解题。。。
[/Quote]
baby1986 2012-03-26
  • 打赏
  • 举报
回复
SELECT COUNT(ORDERID)
FROM ORDERITEM
GROUP BY ORDERID, NAME
HAVING NAME = 'Iphone';
hckxzy 2012-03-26
  • 打赏
  • 举报
回复
请大侠继续发帖解题。。。
田小瘦 2012-03-26
  • 打赏
  • 举报
回复
#141.。。。。。
hckxzy 2012-03-26
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 xieshengjun2009 的回复:]

答案已存在!
[/Quote]
那个已存在,请指点。。
hckxzy 2012-03-26
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 flagiris 的回复:]

以上都不对,1L和4L结果一样,不符合要求,
2L和3L压根语法有错。。。。

话说我想起来的也就

select count(0) from (select distinct orderId from OrderItem where name = 'Iphone')

这不就是一条sql语句么。
[/Quote]
这是两条sql。。。
loveofmylife 2012-03-26
  • 打赏
  • 举报
回复
2张表都要用是吧
select count(*) from Order t where exists (select 1 from OrderItem where name ='Iphone' and id = t.id)
xieshengjun2009 2012-03-26
  • 打赏
  • 举报
回复
答案已存在!
chkaka 2012-03-26
  • 打赏
  • 举报
回复
select count(orderId) from OrderItem where name='Iphone' group by orderId
菖蒲老先生 2012-03-26
  • 打赏
  • 举报
回复
当然跟简单的也可以写成这样


select count(distinct orderId) from OrderItem where name = 'Iphone'
xiongyu2006 2012-03-26
  • 打赏
  • 举报
回复
一楼也是对的吧!

select count(orderId) from OrderItem where name='Iphone';
加载更多回复(4)

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧