0

I'm working on a laravel project and I'am trying to use where with groupBy on my query. The below is example of drug_transactions table.

screen_id     patient_id    drug_id
    1             1            1
    1             1            2
    1             1            3
    2             1            1
    3             2            1

I would like to get screen_id for patient_id = 1. Then I use eloquent as below.

    $drugtransactiongroups = DrugTransaction::where('patient_id',1)
                                        ->groupBy('screen_id')
                                        ->get();

My need result should return screen_id= 1 and 2. But I have an error as below instead.

SQLSTATE[42000]: Syntax error or access violation: 1055 'zigmaclinic.drug_transactions.id' isn't in GROUP BY (SQL: select * from drug_transactions where patient_id = 1 group by screen_id)

Any advice or guidance on this would be greatly appreciated, Thanks.

1

1 Answer 1

2

You only can select fields that was used within groupBy

DrugTransaction::select('screen_id')
    ->where('patient_id',1)
    ->groupBy('screen_id')
    ->get();
Sign up to request clarification or add additional context in comments.

1 Comment

Dear Hung Nguyen, Thank you very much.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.