简体   繁体   English

返回字段并计数在同一条sql中

[英]Returning fields and count in the same sql

I have a specific query i can't figure out how to get done. 我有一个特定的查询,我不知道如何完成。

I need to return the values of my articles table named Articulos, and count how many times each article is used on every budget. 我需要返回名为Articulos的文章表的值,并计算每个预算在每个预算上使用了多少次。

i have a detail table called pre_detalle which contains a field called idart, where the articulos.id are written in case of being included. 我有一个名为pre_detalle的详细信息表,其中包含一个名为idart的字段,如果包含articulos.id,则会在其中写入。

i need each counter to be returned as cont_num. 我需要每个计数器作为cont_num返回。

this is the query i tried to develop: 这是我尝试开发的查询:

SELECT *, COUNT(articulos.id) as cont_num
FROM articulos
WHERE articulos.id IN(SELECT idart FROM pre_detalle)
GROUP BY articulos.id
ORDER BY cont_num

In most cases, you need to count a list of item that you display. 在大多数情况下,您需要计算显示的项目列表。
In most cases, you will use a pagination system to display your rows of data. 在大多数情况下,您将使用分页系统显示数据行。
In most cases, if you try to do it with the same query, it will simply fail. 在大多数情况下,如果您尝试使用相同的查询来执行此操作,则它只会失败。
So, maybe you should get in mind to do it using one query to count the rows and another one to get the rows you want. 因此,也许您应该记住使用一个查询来对行进行计数,而使用另一个查询来获取所需的行来执行此操作。

In most cases, this answer could help you (in a proper way). 在大多数情况下,此答案可以(以适当的方式)为您提供帮助。

this is what i needed to do, a couple hours later and after lots of tries i managed to get it working. 这是我需要做的,几个小时后,经过多次尝试,我设法使其正常工作。 SELECT articulos. 选择关节。 , (SELECT COUNT( ) FROM pre_detalle WHERE pre_detalle.idart = articulos.id) AS cont_num FROM articulos ,(SELECT COUNT( )从pre_detalle哪里pre_detalle.idart = articulos.id)作为cont_num FROM articulos

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM