I am new to laravel.
I have three tables in my DB
dsp_account with fields id,name_display,short_name.
dsp with fields id,dsp_account_id.
.
rtbcfg_region_dsp with fields dsp_id,status.
.
dsp table connected to dsp_account table via dsp_account_id field.
dsp table connected to rtbcfg_region_dsp via dsp_id.
I try to create this sql query:
select distinct dsp_account.id,dsp_account.name_display,dsp_account.short_name
from dsp_account
inner join dsp on dsp.dsp_account_id = dsp_account.id
AND dsp.id
IN(SELECT dsp_id
FROM rtbcfg_region_dsp
group by dsp_id, status
having count(*)>0 and status = 1) order by dsp_account.name_display ASC;
this is my code:
$all_list6 = DspAccount::select('DspAccount.id', 'DspAccount.name_display','DspAccount.short_name')
->join('dsp', 'dsp.dsp_account_id', '=', 'dspAccount.id')
->whereIn('DSP.id',function($query)
{
$query->select('dsp_id')
->from('rtbcfg_region_dsp')
->groupBy('dsp_id','status')
->havingRaw('COUNT(*) > 0 AND status = 1');
})
->get();
this is the error that I got:
what I am missing:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'DspAccount.id' in 'field list' (SQL: select `DspAccount`.`id`, `DspAccount`.`name_display`, `DspAccount`.`short_name` from `dsp_account` inner join `dsp` on `dsp`.`dsp_account_id` = `dspAccount`.`id` where `DSP`.`id` in (select `dsp_id` from `rtbcfg_region_dsp` group by `dsp_id`, `status` having COUNT(*) > 0 AND status = 1) and `dsp_account`.`deleted_at` is null)
$all_list6 = DspAccount::select('dsp_account.id', 'dsp_account.name_display','dsp_account.short_name')
->join('dsp', 'dsp.dsp_account_id', '=', 'dsp_account.id')
->whereIn('dsp.id',function($query)
{
$query->select('dsp_id')
->from('rtbcfg_region_dsp')
->groupBy('dsp_id','status')
->havingRaw('COUNT(*) > 0 AND status = 1');
})
->distinct()->get();
You should use original table name dsp_account instead of DspAccount as it is model class name. I haven't debug the query but i answered about only syntax error. Thanks:)
Related
I have the problem with pagination in laravel framework.
I built my query in this way:
$auctions = Auction::where('status', '=', 1)
->Where('minQuantity', '>', 0)
->whereRaw('endTime > NOW()');
$auctions->selectRaw('*,' . DB::raw('IF ((SELECT COUNT(*) FROM observed_auctions WHERE observed_auctions.idUser=' . intval($userId) . ' AND observed_auctions.idAuction=auctions.id)>0,\'true\',\'false\') as isObserved'));
Next, I get the result:
$auctions->paginate(15, ['name'], 'page', $currentPage);
It works pretty well until I want to find only observed auctions if I added where clause:
$auctions->where('isObserved', '=', true);
I get error:
Column not found: 1054 Unknown column 'isObserved' in 'where clause' (SQL: select count(*) as aggregate from auctions where status = 1 and minQuantity > 0 and endTime > NOW() and isObserved = 1)
How can I solve this problem?
SELECT COUNT(*) as count, MONTH(begin_date)
FROM `events`
WHERE (YEAR(begin_date) = YEAR(CURDATE()))
OR (YEAR(begin_date) = YEAR(CURDATE()) + 1)
GROUP BY MONTH(begin_date)
Here is sql query, i want to write it in laravel eloquent.
what i try:
$oncoming_events = DB::table('events')
->select(DB::raw('count(*) as numOfOncomingEvents, MONTH(begin_date)'))
->where('YEAR(begin_date)', '=', 'YEAR(CURDATE())')
->orWhere('YEAR(begin_date)', '=', 'YEAR(CURDATE()) +1')
->groupBy('MONTH(begin_date)')->get();
Error:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'YEAR(begin_date)' in 'where clause' (SQL: select count(*) as numOfOncomingEvents, MONTH(begin_date) from events where YEAR(begin_date) =
laravel 5.6
btw
sql query works..
You need to use DB::raw() in where to tell the query builder that it is not column name it is data manipulation in query,
$oncoming_events = DB::table('events')->select(DB::raw('count(*) as numOfOncomingEvents, MONTH(begin_date)'))->where(DB::raw('YEAR(begin_date)'), '=', 'YEAR(CURDATE())')->orWhere(DB::raw('YEAR(begin_date)'), '=', 'YEAR(CURDATE()) +1')->groupBy(DB::raw('MONTH(begin_date)'))->get();
I'm trying to use join() in laravel5.1 but I'm getting this error:
QueryException in Connection.php line 666: SQLSTATE[23000]: Integrity
constraint violation: 1052 Column 'id' in where clause is ambiguous
(SQL: select * from quizzes inner join question_numbers on
question_numbers.quiz_id = quizzes.id and
question_numbers.question_id = multiple_choice.id inner join
multiple_choice on multiple_choice.id =
question_numbers.question_id where id = 1)
I have three tables here and I want to join these three tables. What I want first is to get a quiz where id = to a specific id then join the table question_numbers where question_numbers.quiz_id is = to the quiz.id then join the table multiple_choice where multiple_choice.id is = question_numbers.question_id and where question_numbers.question_type = multiple_choice.
What I have:
$quiz = DB::table('quizzes')->where('id', $id)
->join('question_numbers', function($join){
$join->on('question_numbers.quiz_id', '=', 'quizzes.id')
->on('question_numbers.question_id', '=', 'multiple_choice.id');
})
->join('multiple_choice', 'multiple_choice.id', '=', 'question_numbers.question_id')
->get();
dd($quiz);
First, you need to set a table aliaslike this quizzes as t for your table that belongs the where condition's id (I assume you meant quizzes table id column here) and then if you refer it inside where like where('t.id', $id), it will not complain about the ambiguous integrity because other table also have id column that's why it is showing you the below Query exception,
Column 'id' in where clause is ambiguous
Try like this,
$quiz = DB::table('quizzes as t')->where('t.id', $id)
->join('question_numbers', function($join){
$join->on('question_numbers.quiz_id', '=', 't.id');
})
->join('multiple_choice', 'multiple_choice.id', '=', 'question_numbers.question_id')
->get();
dd($quiz);
I'm trying to build in search functionality in my application (Laravel 5.1), but my join doesn't seem to do anything to the resulting query. What am I doing wrong?
Code:
$query = InvoiceHeader::where('customer_code_id', '=', Auth::user()->customer_code_id);
$query->join('invoice_types', 'invoice_headers.invoice_type_id', '=', 'invoice_types.id')
->where('invoice_types.name', '<>', array_search('Faktura', InvoiceHeader::INVOICE_TYPES));
$invoices = $query->paginate(15);
Resulting query:
select count(*) as aggregate from invoice_headers where customer_code_id = 1 and (invoice_types.name <> 380)
Resulting response:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'invoice_types.name' in 'where clause'
This is the query I was hoping to see:
select count(*) as aggregate
from invoice_headers
inner join invoice_types
on invoice_headers.invoice_type_id = invoice_types.id
where customer_code_id = 1
and (invoice_types.name <> 380)
$query = InvoiceHeader::where('customer_code_id', '=', Auth::user()->customer_code_id);
you need to store the query in a variable.
$query = $query->join('invoice_types', 'invoice_headers.invoice_type_id', '=', 'invoice_types.id')
->where('invoice_types.name', '<>', array_search('Faktura', InvoiceHeader::INVOICE_TYPES));
$invoices = $query->paginate(15);
You need to add JOIN with invoice_types table if you want to filter on invoice_types.name.
I'm trying to use a result of subquery in where clause , but I failed
I'm using a laravel db class
this is my code
DB::table('sales as SL')
->join('prsn as PR1','PR1.id_person','=','SL.customer_id')
->join('prsn as PR2','PR2.id_person','=','SL.investor_id')
->select('SL.customer_id' ,'SL.investor_id','SL.type_agd','SL.status','SL.id_agd','PR1.full_name_person as Buyer'
,'PR2.full_name_person as Seller','PR1.mobile1_person as Mobile',
DB::raw('(select SUM(money_operation) from opers where agd_operation = SL.id_agd AND ty IN ("3","4","5","6","7","8","9","10","12") AND status = 1 ) as Madfoo3'),
DB::raw('(select SUM(money) from inst where agd_id = SL.id_agd AND date <= "'.DT::Today('Ymd').'" AND type != "D") as Matloob'),
DB::raw('(select (Matloob - Madfoo3 )) Mutakher' ))
->whereRaw('(Mutakher > 0)')
->paginate();
this error appear:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Mutakher' in 'where clause' (SQL: select count(*) as aggregate from `sales` as `SL` inner join `prsn` as `PR1` on `PR1`.`id_person` = `SL`.`customer_id` inner join `prsn` as `PR2` on `PR2`.`id_person` = `SL`.`investor_id` where (Mutakher > 0))
/var/www/Taqseet/vendor/laravel/framework/src/Illuminate/Database/Connection.php#625
I want to make a condition if the "Mutakher" > 0 give me the raw.
Thanks
I solve this problem
DB::table('sales as SL')
->join('prsn as PR1','PR1.id_person','=','SL.customer_id')
->join('prsn as PR2','PR2.id_person','=','SL.investor_id')
->select('SL.customer_id' ,'SL.investor_id','SL.type_agd','SL.status','SL.id_agd','PR1.full_name_person as Buyer'
,'PR2.full_name_person as Seller','PR1.mobile1_person as Mobile',
DB::raw('(select SUM(money_operation) from opers where agd_operation = SL.id_agd AND ty IN ("3","4","5","6","7","8","9","10","12") AND status = 1 ) as Madfoo3'),
DB::raw('(select SUM(money) from inst where agd_id = SL.id_agd AND date <= "'.DT::Today('Ymd').'" AND type != "D") as Matloob'),
DB::raw('(select (Matloob - Madfoo3 )) Mutakher' ))
->whereRaw('((Matloob - Madfoo3 ) > 0)')
->paginate();