Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql Django:按天分组和计数_Mysql_Django_Sqlite_Postgresql - Fatal编程技术网

Mysql Django:按天分组和计数

Mysql Django:按天分组和计数,mysql,django,sqlite,postgresql,Mysql,Django,Sqlite,Postgresql,我需要按日期对项目进行分组和计数。我在sqlite上获得了以下信息: Books.objects.filter(state="new").extra({"published_at": "date(published_at)"}).values("published_at").annotate(counter=Count("pk")) 但是,在sql语句中使用extra()不能移植到其他dbms上。例如,上述内容在mysql上不起作用。 我如何能够为sqlite、postgresql和mysql

我需要按日期对项目进行分组和计数。我在sqlite上获得了以下信息:

Books.objects.filter(state="new").extra({"published_at": "date(published_at)"}).values("published_at").annotate(counter=Count("pk"))
但是,在sql语句中使用extra()不能移植到其他dbms上。例如,上述内容在mysql上不起作用。 我如何能够为sqlite、postgresql和mysql创建一个有效的查询?

如果您想要一个数据库无关的查询,那么就不要使用extra()。 books=books.objects.filter(state=“new”) .订购人('published_at',) .值(“发布于”,) .annotate(count=count(“发布日期”))

访问日期(在日期发布)失败,原因如下:

FieldError:不允许在“日期”字段加入。你把“day”拼错了吗 查找类型

如果重写为按天分组

Book.objects.all().extra(select={'day': 'extract( day from date )'}).values('day').annotate(num=Count('date')).order_by()
您可能希望将.all()替换为.filter(year=some_year),因为这将跨越多年


Extract
已确认可在MySQL和PostgreSQL上运行。

您如何按日期计数。看起来你是在按pk计数,pk总是1。另外,为什么需要
“日期(发布时间)”
。Django的
Datetime
字段已经提供了一个Datetime对象,因此无需再次转换为日期。因为published_at是一个时间戳,我需要按天计算书籍。更新了我的答案。但我仍然不确定我是否完全理解我试图实现的目标。请详细说明。SQLite.extra({“published_at”:“date(published_at)”})做什么?将字符串转换为日期?是的,它转换为日期。正如我所说,我需要每天出版书籍的计数器。此代码得到以下错误:不允许在字段“published_at”上加入。查找类型是否拼错了“day”?