gpt4 book ai didi

How can I get the data used as key for group by(如何将数据用作GROUP BY的密钥)

转载 作者:bug小助手 更新时间:2023-10-25 19:46:39 29 4
gpt4 key购买 nike



id num tuser
1 2 user1
2 2 user2
3 3 user2
4 1 user4
5 1 user4

For example I have table like this .

例如,我有一张这样的桌子。


Now I want to get the unique data appeared in tuser so, the result should be
user1,user2,user4

现在,我希望获得出现在tUSER中的唯一数据,因此,结果应该是USER1、USER2、USER4


I guess it should be related with group by. So, I made this sentence.

我想这应该和GROUP BY有关。所以,我写了这句话。


p = m.MyTables.objects.all().values('tuser')

However how can I get the user1,user2,user4 ?

但是,我如何才能获得用户1、用户2、用户4呢?


更多回答

Why use GROUP BY? Seems to be a better use case for DISTINCT

为什么使用group by?DISTINCT似乎是一个更好的用例

Because Distinct doesnt work in my mysql.

因为DISTINCT在我的MySQL中不起作用。

优秀答案推荐

It depends on what you want. Do you like to make the sum of the num column? If yes please use:

这取决于你想要什么。你想把数字列的总和算出来吗?如果是,请使用:


select sum(num), tuser from table group by tuser;

Instead of sum you can use each aggregation function like avg, min, max, etc.

除了SUM,您可以使用每个聚合函数,如avg、min、max等。


If you jus want to have a list of all unique tusers please use:

如果您希望获得所有唯一tuser的列表,请使用:


select distinct(tuser) from table;


I use count instead of distinct and it works.

我使用Count而不是DISTINCT,它起作用了。


Because distinct('something') works only on potgres, mysql not.

因为DISTINCT(‘Something’)只能在potgres上工作,而MySQL不能。


   m.MyTable.objects
values('tuser').
annotate(dcount=Count('tuser')).
order_by())

Thank you very much for your help.

非常感谢你的帮助。


更多回答

Thank you, however mysql doesn't use distinct('something') postgres can.

谢谢,但是mysql不使用distinct('something')postgres可以。

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com