注册 登录
编程论坛 SQLite论坛

查询时如何过滤掉重复记录?

smooth 发布于 2019-12-22 21:07, 1883 次点击
请教各位大神,如下查询语句,
SELECT * FROM shuju WHERE (fengxiang='字符') AND (fengxu=33) AND (juli=19 OR juli=21 OR juli=22 OR juli=23 OR juli=24 OR juli=26 OR juli=27) ORDER BY juli
如何能让条件:
juli=19 OR juli=21 OR juli=22 OR juli=23 OR juli=24 OR juli=26 OR juli=27
符合以下要求:
1、中心距离,juli=23,如果大于3条记录,则只查询3条记录,否则都查询出来。23通过变量$juli可以得到;
2、其他 juli=19 OR juli=21 OR juli=22 ,以及juli=24 OR juli=26 OR juli=27,则只查询一条出来即可,忽略重复记录。
2 回复
#2
smooth2019-12-23 13:50
输入查询条件后结果如附件图片:
只有本站会员才能查看附件,请 登录

row6,称之为中心记录,这个肯定是有的,row6之后的记录,有可能有,也可能没有。row6之前的记录,也可能有,也可能没有。
现在要想实现的效果是:
row6之前的记录:从row6往前推,如果col2列的值重复,就丢弃。
row6之后的记录:从row6往后推,如果col2列的值与row6相同的,保留2个(含row6则为保留3个记录),再之后的记录:那些col2列的值与row6对应的col2的值不同的,遇到重复的就丢弃。
#3
smooth2019-12-23 22:33
解决了:
程序代码:

;删除中心组记录大于3的部分
                            Local $q, $j, $z
                            For $i = 0 To UBound($aResult) - 1
                                If $aResult[$i][2] = $juli Then
                                    $q = $i
                                    $j = $j + 1
                                EndIf
                            Next
                            $z = $q - $j + 1
                            If $j > 3 Then
                                For $i = $z + $j - 1 To $z + 3 Step -1
                                _ArrayDelete($aResult, $i)
                                Next
                            EndIf   
                           
                            ;删除中心之前的重复记录
                            Local $e
                            For $i = 0 To UBound($aResult) - 1
                                If $aResult[$i][2] = $juli Then
                                    $e = $i
                                    ExitLoop
                                EndIf
                            Next
                            If $e > 1 Then
                                For $i = $e - 1 To 1 Step -1
                                    If $aResult[$i][2] = $aResult[$i - 1][2] Then
                                        _ArrayDelete($aResult, $i - 1)
                                    EndIf   
                                Next
                            EndIf
                           
                            ;删除中心之后的重复记录
                            Local $f
                            For $i = 0 To UBound($aResult) - 1
                                If $aResult[$i][2] = $juli Then
                                    $f = $i
                                EndIf
                            Next
                            For $i = UBound($aResult) - 1 To $f + 1  Step -1
                                If $aResult[$i][2] = $aResult[$i - 1][2] Then
                                    _ArrayDelete($aResult, $i)
                                EndIf   
                            Next
1