SQL 查询在子查询中查找最大行
这些是我的表:
User, Product, DiscountGroup, DiscountUser, DiscountProduct.
DiscountProduct:
id discountGroupId productId discount
--- -------------- --------- -------
1 2 8 2000
2 3 8 1000
3 2 4 4500
DiscountUser:
id discountGroupId userId
--- -------------- ---------
1 2 2
2 3 3
3 2 2
DiscountGroup:
id title active
--- ------ --------
1 A 1
2 B 0
3 C 1
我使用 SQL Server 2000。
我想要的:
首先:对于每个productid和成员,找到它们都属于它的discountGroup。
我写下我的查询:
select *
from discountGroup
where id in (select discountgroupId
from discountproduct
where productid = 11)
and id in (select discountgroupId
from discountuser
where userid = 2)
and active = 1
第二:我想找到特殊产品和会员的最大折扣。
我该怎么做呢?
第三:对于特殊用户和所有产品我想找到最好的折扣和折扣组标题:
相同:
user produc discount discountGroup
--- ----- ------- ------------
ali phone 400 A
reeza mobile 200 B
These are my tables:
User, Product, DiscountGroup, DiscountUser, DiscountProduct.
DiscountProduct:
id discountGroupId productId discount
--- -------------- --------- -------
1 2 8 2000
2 3 8 1000
3 2 4 4500
DiscountUser:
id discountGroupId userId
--- -------------- ---------
1 2 2
2 3 3
3 2 2
DiscountGroup:
id title active
--- ------ --------
1 A 1
2 B 0
3 C 1
I use SQL Server 2000.
What I want :
first: for each productid and member find discountGroup that both of Them belong to it.
I write my query:
select *
from discountGroup
where id in (select discountgroupId
from discountproduct
where productid = 11)
and id in (select discountgroupId
from discountuser
where userid = 2)
and active = 1
Second: I want to find maximum discount for special product and member.
How can I do it?
Third: for special user and all product I want to find the best discount and discountGroup Title:
Same this:
user produc discount discountGroup
--- ----- ------- ------------
ali phone 400 A
reeza mobile 200 B
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不要使用子查询,使用联接:
要获得最大折扣,请使用最大聚合:
Don't use subqueries, use joins:
To get the maximum discount, use the max aggregate: