如何从下表获得结果

时间:2019-09-10 09:37:08

标签: mysql

表格:

PNumber       PGen  PRev

Testnumber1   10    02
Testnumber1   8     01
Testnumber1   2     00
Testnumber1   3     00
Testnumber1   4     00
Testnumber1   5     00
Testnumber1   1     00
Testnumber1   7     00
Testnumber1   9     01
Testnumber9   1     00
Testnumber1   6     00

我想要这样的结果:

PNumber       PGen     PRev
---------------------------
Testnumber1   7        00
Testnumber1   8        01
Testnumber1   10       02
Testnumber9   1        00

i.e希望获得最大的PGen,以00等。

1 个答案:

答案 0 :(得分:0)

听起来像您需要带有一些聚合的GROUP BY查询。

class Register(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    address = models.CharField(max_length=70)
    phone = models.IntegerField()
    register_as = models.CharField(max_length=20)


# register serializer
class RegisterSerializer(serializers.ModelSerializer):
    model = Register
    fields = ('id', 'username', 'email', 'address', 'phone', 'register_as', 'password')
    extra_kwargs = {'password': {'write_only':True}}

    def create(self, validated_data):
        user = User.objects.create_user(validated_data['username'], validated_data['email'], validated_data['password'])

        return user


#register API
class RegisterAPI(generics.GenericAPIView):  
    serializer_class = RegisterSerializer

    def post(self, request, *args, **kwargs):  

        serializer = self.get_serializer(data=request.data) 
        serializer.is_valid(raise_exception=True)
        user = serializer.save()

        return Response({                                  
        'user': UserSerializer(user, 
        context=self.get_serializer_context()).data,  
        'token': AuthToken.objects.create(user)[1] 
        })