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();
Related
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:)
I'm using Doctrine ORM with PHP and with MariaDB. I'm trying to fetch only the paid orders from the database. Here's my code:
public function listOrders($filter = [], $limit = 50, $offset = 0): array {
$qb = $this->entityManager->createQueryBuilder();
$page = isset( $filter['page'] ) ? $filter['page'] - 1 : $offset;
$perPage = $filter['perPage'] ?? $limit;
$firstResult = $page * $perPage;
$orderBy = isset($filter['sortBy'])
? 'orders.'.$filter['sortBy']
: 'orders.created';
$qb
->select('orders')
->from(OrderData::class, 'orders')
->setFirstResult($firstResult)
->setMaxResults($perPage)
->orderBy($orderBy, isset($filter['sortDesc']) ? 'ASC' : 'DESC ');
// Filter by orders that are paid
if (isset($filter['paid']) && $filter['paid']) {
$qb
->leftJoin('orders.items', 'item')
->addSelect('SUM(item.price) AS item_prices')
->leftJoin('orders.transactions', 'transaction')
->addSelect('SUM(transaction.price) AS paid_amount')
->andWhere(
$qb->expr()->eq(
'paid_amount',
'item_prices'
) // Get only paid orders
);
}
...
}
The error I'm getting:
An exception occurred while executing
SELECT
DISTINCT id_0
FROM
(
SELECT
DISTINCT id_0,
created_2
FROM
(
SELECT
o0_.id AS id_0,
o0_.token AS token_1,
o0_.created AS created_2,
o0_.updated AS updated_3,
o0_.requestId AS requestId_4,
SUM(o1_.price) AS sclr_5,
SUM(o2_.price) AS sclr_6,
o1_.price AS price_7,
o2_.price AS price_8,
o1_.type AS type_9
FROM
orders o0_
LEFT JOIN orders_items o1_ ON o0_.id = o1_.order_id
LEFT JOIN orders_items_journeys o3_ ON o1_.id = o3_.id
LEFT JOIN orders_items_railpasses o4_ ON o1_.id = o4_.id
LEFT JOIN orders_items_articles o5_ ON o1_.id = o5_.id
LEFT JOIN orders_items_distributions o6_ ON o1_.id = o6_.id
LEFT JOIN orders_items_giftcard o7_ ON o1_.id = o7_.id
LEFT JOIN orders_transactions o2_ ON o0_.id = o2_.order_id
WHERE
sclr_6 = sclr_5
ORDER BY
o2_.created DESC
) dctrn_result_inner
ORDER BY
created_2 DESC
) dctrn_result
LIMIT
30
':SQLSTATE[42S22]: Column not found: 1054 Unknown column 'sclr_6' in 'where clause'
The syntax is valid, but it can not recognize the generated column for some reason.
Any help would be appreciated.
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?
I want to obtain the first_check_in and last_check_out of an employee for the current day. The following MySQL code is working good but I am new to eloquent and I do not know how to write it. sorry for my English.
SELECT `employees`.`*`,
`teams`.`description`,
`time_groups`.`start`,
`time_groups`.`end`,
cast(a1.action_time as date) AS date,
Min(`a1`.`action_time`) AS `first_check_in`,
MAX(`a2`.`action_time`) AS `last_check_out`
FROM `employees`
JOIN `teams`
ON `employees`.`team_id` = `teams`.`id`
JOIN `time_groups`
ON `teams`.`time_group_id` = `time_groups`.`id`
JOIN `attendance_employees` AS a1
JOIN `attendance_employees` AS a2
ON `employees`.`id` = `a2`.`employee_id`
AND `a1`.`employee_id` = `a2`.`employee_id`
AND DATE(a1.action_time) = DATE(a2.action_time)
WHERE 1 = 1
AND `a1`.`type` = 1
AND `a2`.`type` = 2
AND DATE(a1.action_time) = CURDATE()
GROUP BY a1.employee_id, `date`
I have try sometime like that
DB::table('employees')
->join('teams', 'employees.team_id', '=', 'teams.id')
->join('time_groups', 'teams.time_group_id', '=', 'time_groups.id')
->join('attendance_employees as a1')
->join('attendance_employees as a2',
function($join) {
$join->on('employees.id', '=', 'a2.employee_id');
$join->on('a1.employee_id', '=', 'a2.employee_id');
$join->on('DATE(a1.action_time)', '=', 'DATE(a2.action_time)');
}
)
->select(
'employees.*',
'teams.description',
'time_groups.start',
'time_groups.end'
)
->addSelect('cast(A1.action_time as date)')->alias('date')
->addSelect('a1.action_time')->alias('check_in')
->addSelect('a2.action_time')->alias('check_out')
->where(1,1)
->where('a1.type', 1)
->where('a2.type', 2)
->groupBy('employee_id')
->groupBy('date')
But getting the following error:
SQLSTATE[42S02]: Base table or view not found: 1146 Table
'db.attendance_employees as a1' doesn't exist
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.