get查询mysql中的最小记录

zysjyyx4  于 2021-06-20  发布在  Mysql
关注(0)|答案(2)|浏览(259)

大家好,我是新的查询和dnt有多少想法,我们如何才能实现这一点
我有两个表产品和定制产品。自定义产品hv productid和计数编号。

id :  pid :  count : belong 
1      200      5 .   1
2      201      2     1
3      203      0 .   1
4      204      6 .   2

产品表包含产品信息,如id名称描述等
我试图做一个查询,以获得他们所属的所有产品和最小计数数必须大于零。
在这里,我希望产品标识201作为结果。
我试着在querie下面

Select *
from product as e inner join
    `customproducts` AS `u`
     on u.product_id = e.id

但这里我只得到customproducts表中的产品hv记录

t9eec4r0

t9eec4r01#

尝试使用where cluase:

Select *
from product as e inner join
    `customproducts` AS `u`
     on u.product_id = e.id where count>0
sqxo8psd

sqxo8psd2#

可以将联接与子查询一起使用,使最小计数大于0

Select *
from product as e 
inner join customproducts AS u  on u.product_id = e.id
inner join (
  select  min(count)  from customproducts min_count
  where count > 0
)  t on t.min_count  = u.count

对于同一个id

Select *
from product as e 
inner join customproducts AS u  on u.product_id = e.id
inner join (
  select belong, min(count) min_count from customproducts 
  where count > 0
  group by belong
)  t on t.min_count  = u.count and t.belong= u.belong

相关问题