在查询mysql

时间:2018-08-24 11:14:10

标签: mysql sql join left-join

亲爱的人们,我是新来的查询人员,而dnt知道如何实现这一目标

我有两个表产品和定制产品。自定义产品h productid和计数编号。

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

产品表包含产品信息,例如ID名称描述等

我正在尝试查询所有属于他们的产品,并且最小数量必须大于零。

在此,我想要产品ID 201。

我正在尝试查询

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

但是在这里,我在customproducts表中仅获得产品hv记录

2 个答案:

答案 0 :(得分:0)

尝试在以下位置使用:

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

答案 1 :(得分:0)

您可以对最小计数> 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