0

I'm doing a simple join query of job_roles and operations table. The fk is the column job_role_id of operations references to the column id of job_roles.

Output that I need is get the total count of data from operations where operations.structure_cost is not equal to job_roles.cost_type

I have a working query created in MySQL Workbench using this code below:

SELECT COUNT(*) as total
   FROM
job_roles t1
   RIGHT JOIN
operations t2 ON t2.job_role_id = t1.id
   WHERE
t2.structure_cost != t1.cost_type
t2.master_id = 1; ### get operations only for auth user

Below is my query in Laravel that don't work. Count that I'm getting in below data example is 3 which is wrong, Count should return 1 as what is indicated in the where clause

enter image description here

table name: job_roles

enter image description here

table name: operations

enter image description here

1
  • Please paste the code as text, not as an image Commented Nov 8, 2022 at 14:46

1 Answer 1

1

Third parameter of where function is used as a value and not as a column name. Use whereRaw function

Sign up to request clarification or add additional context in comments.

2 Comments

Don't even need to use whereRaw(); Laravel has a whereColumn() method: laravel.com/docs/9.x/queries#additional-where-clauses (scroll down a bit)
Thanks @Nick! I'm trying working on using DB::raw but no good. This works.

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.