How to write subquery IN conditional zend 2 - php

I have MySQL Query i want to write this query zend 2.
select billsec, call_status, Count(*)
from (
select
billsec,
if(ANSWERED_NUM is Null, 'Missed', 'Answered') call_status
from cust_info
where billsec in (
select id
from `users`
where `account_id` = 452 and `added_by` = 20694 and `status` = 'active'
)
)a
group by a.billsec,a.call_status
in zend 2 am trying to write like this but
$adapter = $this->getAdapter();
$resultset = $this->select( function( Select $select ) use ( $request, $adapter ) {
$sub1 = new Select( 'users' );
$sub1->columns( array( 'id' ) );
$sub1->where( array( 'account_id' => '452','added_by' => '20694','status' => 'active' ) );
$sub2 = new Select( 'cust_info' );
$sub2->columns(array("id","billsec", "if(ANSWERED_NUM is Null, 'Missed','Answered')"=>"call_status"));
$sub2->where('billsec IN(?)', new \Zend\Db\Sql\Expression( '?', array( $sub1 ) ));
var_dump( $sub2->getSqlString( $adapter->getPlatform() ) );die();
});
When am print this query output like this:
"SELECT `cust_info`.`id` AS `id`, `cust_info`.`billsec` AS `billsec`, `cust_info`.`call_status` AS `if(ANSWERED_NUM is Null, 'Missed','Answered')` FROM `cust_info` WHERE billsec IN('')"
Here am not able to write IN condition Query, thank in advance.

Related

how to search using the sum of case when in cakephp

I want to do implement a search in the generated data of the sum count of a case when in cakephp with or or conditions in each data.
SELECT `Branches`.`name` AS `Branches__name` , `Branches`.`code` AS `Branches__code` , (
COUNT(
CASE WHEN `prodCarried`.`carried` =1
THEN 1
END )
) AS `carried` , (
COUNT(
CASE WHEN `prodCarried`.`carried` =0
THEN 1
END )
) AS `unCarried`
FROM `branches` `Branches`
INNER JOIN `products_carried_per_branches` `prodCarried` ON ( prodCarried.company_id = Branches.company_id
AND prodCarried.branch_code = Branches.code )
WHERE (
`Branches`.`company_id` =200017
AND `Branches`.`deleted` =0
AND `prodCarried`.`deleted` =0
)
GROUP BY `code`
HAVING COUNT(
CASE WHEN `prodCarried`.`carried` =1
THEN 1
END ) =39
the picture shows the mysql result without the having code
while this is my cakephp code, I want to implement the having sql into cakephp or condition to search the generated sum count or search the data name and code. is this possible or nah
$carriedCase = $query->newExpr()
->addCase(
$query->newExpr()->add(['prodCarried.carried' => '1']),
1,
'integer'
);
$unCarriedCase = $query->newExpr()
->addCase(
$query->newExpr()->add(['prodCarried.carried' => '0']),
1,
'integer'
);
//disctinct code
$query ->select([
'name',
'code',
'carried' => $query->func()->count($carriedCase),
'unCarried' => $query->func()->count($unCarriedCase),
'prodCarried.id',
'prodCarried.validity_start',
'prodCarried.validity_end',
]);
$query ->distinct([
'code'
]);
$query->join([
'prodCarried' => [
'table' =>'products_carried_per_branches',
'type' => 'inner',
'conditions' => [
'prodCarried.company_id = Branches.company_id',
'prodCarried.branch_code = Branches.code',
]
]
]);
$query->where(function (QueryExpression $exp, Query $q) use($option,$search){
$exp->eq('Branches.company_id', $option['company_id']);
$exp->eq('Branches.deleted', 0);
$exp->eq('prodCarried.deleted', 0);
if(!empty($search)){
$orConditions = $exp->or_(function (QueryExpression $or) use ($search) {
$or->like('name', "%$search%");
$or->like('code', "%$search%");
//****************************
return $or;
});
$exp->add($orConditions);
}
return $exp;
});
Depending on your DBMS you can refer to the aggregate of the select list:
$query->having([
'carried' => $searchCount,
]);
Otherwise you have to recreate the aggregate:
$query->having(
function (
\Cake\Database\Expression\QueryExpression $exp,
\Cake\ORM\Query $query
) use (
$carriedCase,
$searchCount
) {
return $exp->eq(
$query->func()->count($carriedCase),
$searchCount
);
}
);
See also
Cookbook > Database Access & ORM > Query Builder > Aggregates - Group and Having

Is it Possible to do a datatable search using the generated sumcount of the sum case in CAKEPHP [duplicate]

I want to do implement a search in the generated data of the sum count of a case when in cakephp with or or conditions in each data.
SELECT `Branches`.`name` AS `Branches__name` , `Branches`.`code` AS `Branches__code` , (
COUNT(
CASE WHEN `prodCarried`.`carried` =1
THEN 1
END )
) AS `carried` , (
COUNT(
CASE WHEN `prodCarried`.`carried` =0
THEN 1
END )
) AS `unCarried`
FROM `branches` `Branches`
INNER JOIN `products_carried_per_branches` `prodCarried` ON ( prodCarried.company_id = Branches.company_id
AND prodCarried.branch_code = Branches.code )
WHERE (
`Branches`.`company_id` =200017
AND `Branches`.`deleted` =0
AND `prodCarried`.`deleted` =0
)
GROUP BY `code`
HAVING COUNT(
CASE WHEN `prodCarried`.`carried` =1
THEN 1
END ) =39
the picture shows the mysql result without the having code
while this is my cakephp code, I want to implement the having sql into cakephp or condition to search the generated sum count or search the data name and code. is this possible or nah
$carriedCase = $query->newExpr()
->addCase(
$query->newExpr()->add(['prodCarried.carried' => '1']),
1,
'integer'
);
$unCarriedCase = $query->newExpr()
->addCase(
$query->newExpr()->add(['prodCarried.carried' => '0']),
1,
'integer'
);
//disctinct code
$query ->select([
'name',
'code',
'carried' => $query->func()->count($carriedCase),
'unCarried' => $query->func()->count($unCarriedCase),
'prodCarried.id',
'prodCarried.validity_start',
'prodCarried.validity_end',
]);
$query ->distinct([
'code'
]);
$query->join([
'prodCarried' => [
'table' =>'products_carried_per_branches',
'type' => 'inner',
'conditions' => [
'prodCarried.company_id = Branches.company_id',
'prodCarried.branch_code = Branches.code',
]
]
]);
$query->where(function (QueryExpression $exp, Query $q) use($option,$search){
$exp->eq('Branches.company_id', $option['company_id']);
$exp->eq('Branches.deleted', 0);
$exp->eq('prodCarried.deleted', 0);
if(!empty($search)){
$orConditions = $exp->or_(function (QueryExpression $or) use ($search) {
$or->like('name', "%$search%");
$or->like('code', "%$search%");
//****************************
return $or;
});
$exp->add($orConditions);
}
return $exp;
});
Depending on your DBMS you can refer to the aggregate of the select list:
$query->having([
'carried' => $searchCount,
]);
Otherwise you have to recreate the aggregate:
$query->having(
function (
\Cake\Database\Expression\QueryExpression $exp,
\Cake\ORM\Query $query
) use (
$carriedCase,
$searchCount
) {
return $exp->eq(
$query->func()->count($carriedCase),
$searchCount
);
}
);
See also
Cookbook > Database Access & ORM > Query Builder > Aggregates - Group and Having

cakephp complex queries

How can I transform the SQL statement into the CakePHP Code? I am able to get the group by result. But how can I add the inner Select Count(*) statement?
I've updated the code under "Revised" However, I am getting this error. Parse error: syntax error, unexpected 'INCOMPLETE' (T_STRING), expecting ')'. I am sure I check my open and close brackets.
SQL Statement below
SELECT
`id` ,
`mtpToken` ,
`mtpCreator_id` ,
`school_id` ,
`user_id` ,
`mtpStatus` ,
`created` ,
`modified` ,
(
(SELECT COUNT( * ) FROM mtpScreenings
WHERE (mtpStatus = 'INCOMPLETE' OR mtpStatus = 'HALFWAY') AND mtpToken = `mtpToken` )
) AS `mainStatus`
FROM `mtpScreenings` WHERE `mtpScreenings`.`school_id` = 15 GROUP BY `mtpToken`
CakePHP Code below
$setting = $this->paginate = array(
conditions' => array('MtpScreening.school_id' => 15),
'recursive' => -1,
'fields' => array('MtpScreening.field1'),
'group' => array('MtpScreening.mtpToken'),
'limit' => 1000
);
Revised
$setting = $this->paginate = array(
conditions' => array('MtpScreening.school_id' => 15),
'recursive' => -1,
'fields' => array('SELECT COUNT(*) FROM mtpScreenings
WHERE (mtpStatus = "INCOMPLETE" OR mtpStatus = "HALFWAY") AND mtpToken = `mtpToken`
) AS mainStatus'),
'group' => array('MtpScreening.mtpToken'),
'limit' => 1000
);
Virtual Field
Code example of virtual field:
public $virtualFields = array(
'count' => "SELECT COUNT( * ) FROM MtpScreening
WHERE (MtpScreening.mtpStatus = 'INCOMPLETE' OR MtpScreening.mtpStatus = 'HALFWAY') AND MtpScreening.mtpToken = 'mtpToken'"
);
This code will produce virtual field:
$this->find('all', array(
'fields'=>array(
'MtpScreening.id',
'MtpScreening.mtpToken',
'MtpScreening.mtpCreator_id',
'MtpScreening.school_id',
'MtpScreening.count'/*virtual field*/
)
)
);
Pagination and virtual fields
Since virtual fields behave much like regular fields when doing
find’s, Controller::paginate() will be
able to sort by virtual fields too.

Laravel Query Builder "select" strange behaviour

I have a problem with the following code:
public function index() {
$qObj = \DB::table('customers as c')
->join('companies_has_customers as chc', 'chc.comhc_cus_id', '=', 'c.cus_id')
->join('cuscategory as cc', 'chc.comhc_cca_id', '=', 'cc.cca_id')
->join('transactions as t','t.trn_cus_id', '=','c.cus_id')
->select('t.trn_id');
$qObj->where('comhc_com_id', '=', $this->companyId);
$res = array(
'count' => $qObj->count(),
'items' => $qObj->get()
);
//print_r($this->showLastQuery());
return parent::prepareResponse($res, 200, 'customers');
}
And this piece of code produces following SQL query:
select * from `customers` as `c`
inner join `companies_has_customers` as `chc` on `chc`.`comhc_cus_id` = `c`.`cus_id`
inner join `cuscategory` as `cc` on `chc`.`comhc_cca_id` = `cc`.`cca_id`
inner join `transactions` as `t` on `t`.`trn_cus_id` = `c`.`cus_id`
where comhc_com_id` = 1
QUESTION:
Why QueryBuilder produce select *... instead of select t.trn_id... as is requested in above syntax?
Change the order:
$res = array(
'items' => $qObj->get(),
'count' => $qObj->count()
);
and it will work.
But better, instead of querying DB twice, do this:
$items = $qObj->get();
$res = array(
'items' => $items,
'count' => count($items)
);
The problem with your code is count() method behaviour - it resets the columns property (set with select), that's all.
Put it in $qObj->get(['t.trn_id']) like this:
$res = array(
'items' => $items = $qObj->get(['t.trn_id']),
'count' => count($items) // count the cached $items
);
Instead of select ( Remove select('t.trn_id')).

When I echo a Zend_Db query it contains 'ON Array', why?

Sql Query:
SELECT test.* FROM test JOIN test_shares
WHERE test.test_id = test_shares.test_id
AND test_shares.email_address = '$email'
AND test.url is NOT NULL ORDER BY title ASC;
Rewritten as Zend_Db_Select statement.
$select = $db->select ()
->from ( 'test', 'test.*' )
->join ( 'test_shares', array () )
->where ( 'test.test_id = test_shares.test_id')
->where ( 'test_shares.email_address = ?', '$email')
->where ( 'test.url is NOT NULL')
->order ( 'title' );
echo $select;
Output:
SELECT test.* FROM test JOIN test_shares ON Array
WHERE test.test_id = test_shares.test_id
AND test_shares.email_address = '$email'
AND test.url is NOT NULL ORDER BY title ASC
Please suggest why this 'ON Array' is showing.
Try this:
$select = $db ->select ()
->from ( 'movie', 'movie.*' )
->join(array('movie_shares'), 'movie.movie_id = movie_shares.movie_id');
->where ( 'movie_shares.email_address = ?', '$email')
->where ( 'movie.url is NOT NULL')
->order ( 'title' );
echo $select;
You're supplying an array where a string is expected for the ON clause, so PHP literally prints array() instead of the string.

Categories