Tuesday, April 5, 2016

Querying from the database, we think is very easy and handy, but it will frustrate us when we encounter those unexpected errors, one of those errors in general is the Query Exception, of course we don't know where to find it, because its very broad to us where to trace the root of the errors, one of the reason is this:

SQLSTATE[23000]: Integrity constraint violation: 1052 

this what i got, when i'm trying to query/retrieve with in the 2 tables, and at the end of error statement with ambiguous column from my WHERE clause, here the exact error.

SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'status' in where clause is ambiguous

SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'status' in where clause is ambiguous (SQL: select `id`, `account_number`, `gateway_alias`, `gateway_id`, `volume`, `gateway_url`, `gateway_user`, `portal_url`, `portal_user`, `company`.`name`, `product` from `mids` inner join `company` on `mids`.`company_id` = `company`.`company_id` where `status` = 1) 

since i'm doing inner join here, we need to identify the table source of the columns inside on our WHERE clause, so i done it like this;

return $q->select(
            'id', 'account_number', 'gateway_alias', 'gateway_id', 'volume', 'gateway_url', 'gateway_user',
            'portal_url', 'portal_user', 'company.name', 'product'
        )
            ->join('company', 'mids.company_id', '=', 'company.company_id' )->where('mids.status', $stat )->get();

NOTE: its a simple Laravel query inside a Model Class

hope this will help! :)
Next
This is the most recent post.
Older Post

0 comments:

Post a Comment