如果product_id
按顺序,我们需要获取行。
表(产品)
product_id name
1 Parle
2 Coconut
3 Pizza
4 Colgate
5 Ice Cream
6 Nuts
8 Britania
9 Pepsi
需要输出
product_id name
1 Parle
2 Coconut
3 Pizza
4 Colgate
5 Ice Cream
6 Nuts
product_id - 8 and 9
没有得到,因为它没有顺序。
我的尝试
select distinct t1.*, t1.product_id, (select GROUP_CONCAT(t2.product_id) from product as t2) as p_id
from product t1
having FIND_IN_SET(t1.product_id+1, p_id)
输出量
product_id name
1 Parle
2 Coconut
3 Pizza
4 Colgate
5 Ice Cream
在这种尝试中,我不会
product_id - 6
争吵。注意:我要
MySql
查询不在PHP
。
谢谢 !
我能想到的一种方法是让用户用户定义的变量获取行的排名,然后计算产品ID和排名的差,并仅选择那些差= 0的行。
select *
from(
select f.*,@row:= @row + 1 rank,
product_id - @row as diff
from product f,
(select @row:= 0) t
order by product_id
) t1
where diff = 0
或者,如果您要选择最少的序列号。您可以自动将其写为
select *
from(
select f.*,@row:= @row + 1 rank,
product_id - @row as diff
from product f,
(select @row:= (select min(product_id) from product_sale_flag) a) t
order by product_id
) t1
where diff = -1
解释 第一个查询将
product_id
变量的最小值赋给@row
,然后为按升序排列的每一行分配一个等级product_id
,一旦为每一行分配了等级,便计算的原始值product_id
与的差,最后使用结果差检查其位置差异是0,因为这些行遵循顺序,所以得到这些行。希望这有意义
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句