This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

leotrooper14689
leotrooper14689

If I run a query im my database I have this result with 66 records:

Alt text

But when I do the exactly same query at October and try to do a count, it returns only 1. Anyone can help me with this issue?



$count_obra = Db::table('obras_totais_by_program')->where('program_id','=', 76)->groupBy('id')->count();

Last updated

leotrooper14689
leotrooper14689

The problem seems to be on the GroupBy clause. This query runs perfectly, but when I try to merge the duplicated results using group by, the Count() result is always 1



$count_obra = Db::table('obras_totais_by_program')->where('program_id','=', 76);

Last updated

axomat
axomat

It is hard to understand why you would want to groupby on id which will always produce a count of 1 (assuming id is the unique primary index). It seems to me that this is working correctly unless there is something else you have not explained.

leotrooper14689
leotrooper14689

The ID is just an alias from a view.

It is not a primary key, and have duplicated values on my query. I just want to group by this id and get the number of rows returned by the query, (66 as shown on the image).

leotrooper14689
leotrooper14689

I figured out. I need to use a count on a raw distinct like this example:


$count_obra = Db::table('obras_totais_by_program')->count(Db::raw('distinct(contrib_iss_isbn)'));
axomat
axomat

Try using a raw sql query from within october using

$programs = DB::select('select * from obras_totais_by_program where program_id = 76');

Counting the programs array would allow you to check that october is working correctly.

If it is working then print the full $count_obra array from your earlier example and I would expect it to contain 66 entries of 1

Last updated

1-6 of 6

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.