multiple join with two table with two condtions in laravel - mysql

I have notification table and quote_policy table in that 2 id will be similar. notification.policy_id = quote_policy.policy_id and notification.user_id ','=','quote_policy.user_id i have to join in the code. got this error production.ERROR: PDOException: SQLSTATE[42000]: Syntax error or access violation: 1066 Not unique table/alias: 'quote_policy' in. Please help me.
$policyCreate = DB::table('notification')->select('*')
->join('quote_policy','notification.policy_id','=','quote_policy.policy_id' )
->join('quote_policy','notification.user_id ','=','quote_policy.user_id')
->groupBy('notification.id')
->get();

Try this Query:
$policyCreate = DB::table('notification')->select('*')
->join('quote_policy as q1','notification.policy_id','=','q1.policy_id' )
->join('quote_policy as q2','notification.user_id ','=','q2.user_id')
->groupBy('notification.id')
->get();
2nd way:
$policyCreate = DB::table('notification')->select('*')
->join('quote_policy', function($join){
$join->on('notification.policy_id', '=', 'quote_policy.policy_id');
$join->on('notification.user_id','=', 'quote_policy.user_id');
});
->groupBy('notification.id')
->get();

Related

I am trying to join 2 tables and wanna fetch the row if it exists but it show me this error SQLSTATE[42000]: Syntax error or access violation: 1064

I am trying to get 'admin_id' from 'logs' table against each user in withdraws. admin_id would get if "User Verified" event is created in 'logs' table.
Here is my logs table
Logs Table
As i am Beginner in laravel so i try this code to fetch result
$withdrawals = DB::table('withdrawals')
->join('user_accounts AS accounts','withdrawals.user','=','accounts.user_id')
->join('users AS user','user.id','=','withdrawals.user')
->join('admin_logs AS logs','withdrawals.unique_id','=','logs.user_id')
->select('withdrawals.*','accounts.total_deduct','user.awarded_flag','logs.*')
here is problem ->whereExists(function ($query) {
$query->where('logs.user_id', 'withdrawals.unique_id')
->where('logs.event', 'User Verified')
->orderby('logs.id','DESC')->first();
})
->where('user.status','active')
->where('withdrawals.status','Pending')
->where('withdrawals.is_verify', 0)
->orderby('withdrawals.created_at','DESC')
->get();
But it gives me following error
SQLSTATE[42000]: Syntax error or access violation: 1064
Here is the table that i am fetcing
WithDraws Table
Here is the error
I think the problem is in JOIN.. Bcz both tables have multiple records of same unique_ids
finally i solved it by raw query
$withdrawals = DB::select( DB::raw("select `withdrawals`.*, `accounts`.`total_deduct`, `user`.`awarded_flag`,(select admin_logs.admin_id from admin_logs WHERE admin_logs.user_id=withdrawals.unique_id and admin_logs.event='User Verified' ORDER BY admin_logs.id DESC LIMIT 1) as adminid
from `withdrawals`
inner join `user_accounts` as `accounts` on `withdrawals`.`user` = `accounts`.`user_id`
inner join `users` as `user` on `user`.`id` = `withdrawals`.`user`
and `user`.`status` = 'active'
and `withdrawals`.`status` = 'Pending'
and `withdrawals`.`is_verify` = 0 order by `created_at` DESC"));

Unknown column in field list when use raw in Laravel

I have a working mysql query like this:
SELECT mc.cart_id, mc.mystore_user_id, MIN(ci.created_at) AS created_at
FROM dmspro_mys_cart AS mc
INNER JOIN dmspro_mys_cart_item AS ci
ON ci.cart_id = mc.cart_id
WHERE mc.is_noticed = 0 AND ci.created_at < '2019-10-08 07:08:39'
GROUP BY mc.cart_id
And I converted it to query builder in my Laravel project:
public function getMinCreatedAt($cartTime)
{
$oSelect = $this->select("{$this->table}.cart_id", "{$this->table}.mystore_user_id",\DB::raw('MIN(ci.created_at) AS created_at'))
->join('cart_item AS ci', 'ci.cart_id', '=', "{$this->table}.cart_id")
->where("{$this->table}.is_noticed", '=', 0)
->where('ci.created_at', '<', $cartTime)
->groupBy("{$this->table}.cart_id")
->get();
return $oSelect;
}
But when I run this, I got error:
Column not found: 1054 Unknown column 'ci.created_at' in 'field list'
(SQL: select dmspro_mys_cart.cart_id,
dmspro_mys_cart.mystore_user_id, MIN(ci.created_at) AS created_at
from dmspro_mys_cart inner join dmspro_mys_cart_item as
dmspro_mys_ci on dmspro_mys_ci.cart_id =
dmspro_mys_cart.cart_id where dmspro_mys_cart.is_noticed = 0
and dmspro_mys_ci.created_at < 2019-10-09 15:51:37 group by
dmspro_mys_cart.cart_id)
How I can fix this?
Thank you!
UPDATE: dmspro_mys_ is my prefix
Your error message does not align with your query.
In the error message an alias named "dmspro_mys_ci" is mentioned, but it does not exist in your query.
Can you please double check this?
Basically your are debuggen the wrong query.
Update
As laravel is setup with prefixing database tables, the alias "ci" is also prefixed.
When referencing this alias in a DB::raw() the table/alias will not automatically be prefixed, so you have to do that yourself by changing:
\DB::raw('MIN(ci.created_at) AS created_at')
to:
\DB::raw('MIN(dmspro_mys_ci.created_at) AS created_at')

Column not found: execption in laravel

I am working on some maintaince project where all are sql queries but i want to convert it in to laravel. Here is the below query :-
$members = Member::select('members.id','members.first_name',
'members.surname','members.username','members.password',
'members.email','user_access.active',
DB::raw('SUM( IF (user_access.active = "y", 1, 0) ) as acount'))
->join('user_access','user_access.member_id','=','members.id')
->where('special_access','=','n')
->groupby('user_access.member_id')
->having('acount','>','0')
->orderby('members.id','desc')
->orderby('members.username','ASC')
->orderby('user_access.note','DESC')
->paginate(30);
Its giving me error when i execute the query. below the error in having clause
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'acount' in 'having clause' (SQL: select count(*) as aggregate from `members` inner join `user_access` on `user_access`.`member_id` = `members`.`id` where `special_access` = n group by `user_access`.`member_id` having `acount` > 0)
I guess something like this will help you.
$userAccess = DB::table('user_access')
->where('special_access','=','n')
->where('active','y')
->groupby('member_id');
Member::select([
'members.id',
'members.first_name',
'members.surname',
'members.username',
'members.password',
'members.email',
'user_access.active'
])->joinSub($userAccess, 'user_access', function($join){
$join->on('user_access.member_id', '=', 'members.id');
})->orderby('members.id','desc')
->orderby('members.username','ASC')
->orderBy('user_access.note','asc')
->paginate(30);
OR
$members = Member::select([
'members.id',
'members.first_name',
'members.surname',
'members.username',
'members.password',
'members.email',
'user_access.active'
])->join('user_access', function($join){
$join->on('user_access.member_id', '=', 'members.id')->on('user_access.active', '=', DB::raw('"y"'));
})
->where('special_access','=','n')
->groupby('user_access.member_id')
->orderby('members.id','desc')
->orderby('members.username','ASC')
->orderby('user_access.note','DESC')
->paginate(30);
As apokryfos said in the comments:
The paginator will attempt to get the count of the query and will remove all selects from it when doing so leading to this error.
If you just need records with user_access.active = "y" then you do not need to select them in the first place and then try to filter them out by HAVING

Eloquent - join table on itself

I'm trying to join a table on itself but keep getting errors. Here is my current code. I've also tried Raw statement. Not sure which direction to go in at the moment.
My code:
$Calle = db('VoipBill')
->table('billing')
->join('billing', 'billing.srcnum', '=', 'billing.dstnum')
->where('acct_name', '100080_company')
->where('srcnum', $call->srcnum)
->whereBetween('calldate', [$set_time_lower, $set_time_upper])
->get();
This is the error:
SQLSTATE[42000]: Syntax error or access violation: 1066 Not unique
table/alias: 'billing' (SQL: select * from billing inner join
billing on billing.srcnum = billing.dstnum where acct_name
= 100080_company and srcnum = +27******** and calldate between 2016-05-02 09:19:27 and 2016-05-02 09:19:37)
You have to alias the tables. Try it it this way:
$Calle = db('VoipBill')
->table('billing as bsrc')
->join('billing as bdst', 'bsrc.srcnum', '=', 'bdst.dstnum')
->where('bsrc.acct_name', '100080_company')
->where('bsrc.srcnum', $call->srcnum)
->whereBetween('bsrc.calldate', [$set_time_lower, $set_time_upper])
->get();

How to make sql join query in laravel 5?

Hi I want to get data from database, I am using join query but I have got this error:
QueryException in Connection.php line 673:
SQLSTATE[42000]: Syntax error or access violation: 1066 Not unique table/alias: 'surat_masuk' (SQL: select jenis_surat.jenis_surat, surat_masuk.nomor_surat, surat_masuk.perihal, surat_masuk.tanggal_publish from surat_masuk inner join jenis_surat on id_jenis_surat = jenis_surat.id_jenis_surat inner join surat_masuk on id_jenis_surat = surat_masuk.id_jenis_surat)
I have 2 tables, surat_masuk and jenis_surat, I want to get jenis_surat field from jenis_surat table and nomor_surat, perihal, tanggal_publish from surat_masuk table. and there is column id_jenis_surat in both table.
This is my query:
$surat = new Surat();
$surats = $surat->join('jenis_surat', 'id_jenis_surat', '=', 'jenis_surat.id_jenis_surat')
->join('surat_masuk', 'id_jenis_surat', '=', 'surat_masuk.id_jenis_surat')
->select('jenis_surat.jenis_surat','surat_masuk.nomor_surat','surat_masuk.perihal','surat_masuk.tanggal_publish')
->get();
Do you know how to fix it?
$surats = $surat->join('jenis_surat', 'surat_masuk.id_jenis_surat', '=', 'jenis_surat.id_jenis_surat')
->select('jenis_surat.jenis_surat','surat_masuk.nomor_surat','surat_masuk.periha‌​l','surat_masuk.tanggal_publish')->get();
$surat = new Surat();
$surats = $surat->join('jenis_surat', 'surat_masuk.id_jenis_surat', '=', 'jenis_surat.id_jenis_surat')
->select('jenis_surat.jenis_surat','surat_masuk.nomor_surat','surat_masuk.perihal','surat_masuk.tanggal_publish')
->get();
OR
$surats = \DB::table('surat_masuk')
->join('jenis_surat', 'surat_masuk.id_jenis_surat', '=','jenis_surat.id_jenis_surat')
->select('jenis_surat.jenis_surat','surat_masuk.nomor_surat','surat_masuk.perihal','surat_masuk.tanggal_publish')
->get();