SQL 查询在子查询中查找最大行

发布于 2024-08-02 09:12:19 字数 1440 浏览 3 评论 0原文

这些是我的表:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

打小就很酷 2024-08-09 09:12:19

不要使用子查询,使用联接:

select g.id, p.discount
from DiscountGroup g
inner join DiscountProduct p on p.discountGroupId = g.id
inner join DiscountUser u on u.discountGroupId = g.id
where p.productid = 11 and u.userid = 2

要获得最大折扣,请使用最大聚合:

select max(p.discount)
from DiscountGroup g
inner join DiscountProduct p on p.discountGroupId = g.id
inner join DiscountUser u on u.discountGroupId = g.id
where p.productid = 11 and u.userid = 2

Don't use subqueries, use joins:

select g.id, p.discount
from DiscountGroup g
inner join DiscountProduct p on p.discountGroupId = g.id
inner join DiscountUser u on u.discountGroupId = g.id
where p.productid = 11 and u.userid = 2

To get the maximum discount, use the max aggregate:

select max(p.discount)
from DiscountGroup g
inner join DiscountProduct p on p.discountGroupId = g.id
inner join DiscountUser u on u.discountGroupId = g.id
where p.productid = 11 and u.userid = 2
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文