0

I'm trying to run this update statement, but I get this error:

SQLSTATE[HY000]: General error: 1 no such column: table2.id

DB::table('table1')
            ->join('table2', 'table1.row_hash', '=', 'table2.row_hash')
            ->where('table1.some_column', '=', 0)
            ->whereNull('table1.reference_no')
            ->update([
                'table1.column_to_update' => 1,
                'table1.column_to_update_2' => 1,
                'table1.column_to_update_3' => 1,
                'table1.reference_no' => DB::raw('table2.id') <--comment this line out and it works.
            ]);

If I comment out that one column from the update statement it works. I've tried using various combinations of quotes and backticks inside of the DB::raw() statement, but still get the same error. This post seems to indicate that I'm doing this the right way, but it's not cooperating.

How can I update the value of table1.reference_no to the value of table2.id? I was hoping to accomplish this in one eloquent query as it's a pretty basic SQL statement. Unfortunately I've also tried using just a raw SQL statement, which yielded other errors despite working when running it directly in my mysql client. This is taking entirely too much time for how simple it should be.

4
  • Your are executing your MySQL update behind an obfuscation layer. If you can, try to print the current (failing) update query to a log file, and see what the syntax problem is. Commented Nov 19, 2022 at 12:38
  • Did you mean to put backticks in the whereNull call? That's the execution operator, so probably returning null. Commented Nov 19, 2022 at 16:11
  • And, this may be obvious, but does table2 / quickbooks_transactions have an id column? Commented Nov 19, 2022 at 16:12
  • @benJ - Sorry, backticks were unintentional, that wasn't causing the error. I updated the post. table2 does have an ID column.. Commented Nov 20, 2022 at 15:50

1 Answer 1

1

Just stumbled over this.

Putting the values in backticks works. Like this:

'table1.reference_no' => DB::raw('`table2`.`id`')

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

Comments

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.