I am not able to convert the below query into Zend_Db_Table_Select query.
SELECT GROUP_CONCAT(wallet_transaction_id) as wallet_transaction_ids,transaction_type,source,status
FROM(
SELECT wallet_transaction_id, transaction_type, source, status
FROM ubiw_transactions_wallet
WHERE (game_id = '1292') AND (player_id = 1538)
ORDER BY date DESC LIMIT 100
) a
GROUP BY a.transaction_type,a.status, a.transaction_type;
any help is appreciated.
thanks,
shiv
// Maybe need some changes
$table = new Zend_Db_Table('ubiw_transactions_wallet');
$subSelect = $table->select()
->from('ubiw_transactions_wallet', array('wallet_transaction_id', 'transaction_type', 'source', 'status'))
->where($table->getAdapter()->quoteInto('game_id = ?', 1292))
->where($table->getAdapter()->quoteInto('player_id = ?', 1538))
->order('`date` DESC')
->limit(100);
$mainSelect = $table->select()
->setIntegrityCheck(false)
->from(array('a' => $subSelect), array('wallet_transaction_ids' => new Zend_Db_Expr('GROUP_CONCAT(wallet_transaction_id)'), 'transaction_type', 'source', 'status'))
->group(array('a.transaction_type', 'a.status', 'a.transaction_type'));
$result = $table->fetchAll($select);
Related
How can I convert this raw query into query builder yii2 as I am getting the record using sub-query:
RAW QUERY:
SELECT *
FROM (
SELECT DISTINCT `comp`.*,TIMESTAMPDIFF(HOUR,comp.created_date,comp.updated_date)
AS hours
FROM `complaints` `comp`
INNER JOIN `complaint_log` `comp_log` ON comp_log.`comp_id` = comp.`id`
WHERE ((comp.name LIKE '%%' OR comp.id LIKE '%%' OR comp.phone_no LIKE '%%')
AND DATE(comp.created_date)
BETWEEN '2018-01-01' AND '2020-03-20')
AND (comp.is_delete = 0)
ORDER BY `comp`.`id` DESC, `comp`.`created_date` DESC)
AS cte_name WHERE hours > 120;
Yii2 Query
$query = (new \yii\db\Query())
->select('comp.*')
->from('complaints comp
->distinct()
->innerJoin('complaint_log as comp_log', 'comp_log.`comp_id` =comp.`id`')
->where($search)
->andWhere('comp.is_delete = 0')
->orderBy("comp.id DESC")
->addOrderBy('comp.created_date DESC');
Try following code:
$subquery = (new \yii\db\Query())
->select(['comp.*', 'TIMESTAMPDIFF(HOUR, comp.created_date, comp.updated_date) AS hours'])
->distinct()
->from('complaints comp')
->innerJoin('complaint_log comp_log', 'comp_log.comp_id = comp.id')
->where(['or',
['like', 'comp.name', $search_name],
['or',
['like', 'comp.id', $search_id],
['like', 'comp.phone_no', $search_phone],
]
])
->andWhere('DATE(comp.created_date) BETWEEN :cStart AND :cEnd', [':cStart' => '2018-01-01', ':cEnd' => '2020-03-20'])
->andWhere(['comp.is_delete' => 0])
->orderBy(['comp.id' => SORT_DESC, 'comp.created_date' => SORT_DESC])
;
$query = (new \yii\db\Query())
->select('cte_name.*')
->from(['cte_name' => $subquery])
->where(['>', 'cte_name.hours', 120])
;
You can use createCommand() function: use below query.
$connection = \Yii::$app->db;
$query = $connection->createCommand(
'
SELECT * FROM (
SELECT DISTINCT comp.*,TIMESTAMPDIFF(HOUR,comp.created_date,comp.updated_date) AS hours
FROM complaints comp
INNER JOIN complaint_log comp_log ON comp_log.comp_id = comp.id
WHERE ('.$search.')
AND (comp.is_delete = 0)
ORDER BY comp.id DESC, comp.created_date DESC
) AS cte_name WHERE hours > 120;
');
$data = $query->queryAll();
Put your raw query in createCommand() function. Here i used $search variable as you used in your sub query
Is there a way to select NULL value or an exact string in a query in YII2?
I am trying to join 3 queries and I need the same number of columns queries and something like "select NULL as returned, submitted, amount, NULL as iamount....."
Example:
$subQuery2 = Loan::find()->select('person_id')->where(['sheet_id' => $id]);
$query2 = new Query();$query2 = new Query();
$query2->select(['last_name','first_name','fc.tax','NULL as returned','fc.submitted','fc.amount','NULL as iamount','NULL as interest',
'b.month','b.year','b.nb'])
->from('sheet as b')
->join('JOIN', 'fee as fc',
'b.id = fc.sheet_id')
->join('JOIN','person','fc.person_id = person.id')
->where(['b.id' => $id])
->andWhere(['not in', 'person_id', $subQuery2]);
$query = new Query();
$query->
select(['last_name','first_name','fc.tax','fi.returned','fc.submitted','fc.amount','fi.amount as iamount','fi.interest',
'b.month','b.year','b.nb'])
->from('sheet as b')
->join('RIGHT JOIN', 'fee as fc',
'b.id = fc.sheet_id')
->join('JOIN','person','fc.person_id = person.id')
->join('LEFT JOIN','loan as fi',
'b.id = fi.sheet_id and fc.person_id = fi.person_id')
->where(['b.id' => $id])
->union($query2)
->orderBy(['last_name' => SORT_DESC]);
Yes, you have two ways to do it:
$query = new Query();
$query->select([
new Expression('NULL as test')
'test2' => new Expression('NULL'),
])->from('sheet as b');
Both of selects will do the same, but the second line (test2) is preferable as it is more DBMS-agnostic.
There is a table "Apartments". Here is a need to create a query in Yii. How to do it?
SQL query:
SELECT *
FROM {{apartments}}
WHERE agent_id = 4
UNION
SELECT *
FROM {{apartments}}
WHERE agent_id != 4
In my cintroller
$arrSql[] = 'SELECT * FROM {{apartments}} WHERE agent_id=:agent_id';
$arrSql[] = 'SELECT * FROM {{apartments}} WHERE agent_id!=:agent_id';
$data = Apartments::model()->findAllBySql(implode(' UNION ', $arrSql), array(
':agent_id' => Yii::app()->user->id,
':status' => Apartments::STATUS_REMOVED
));
$dataProvider = new CArrayDataProvider($data);
But not work pagination and filter in my widget CGridList.
As i know, you can use the CDbCommand like this:
$apartments2 = Yii::app()->db->createCommand()
->select("*")
->from('apartments')
->where('agent_id!=:agent_id', array(':agent_id'=>4))
->getText();
$apartments = Yii::app()->db->createCommand()
->select("*")
->from('apartments')
->where('agent_id=:agent_id', array(':agent_id'=>4))
->union($apartments2)
->queryRow();
you can find your result in the $apartments as an array.
EDIT:
if you want to use a CActiveDataProvider, you need to use CSqlDataProvider:
$sql='SELECT *
FROM {{apartments}}
WHERE agent_id = 4
UNION
SELECT *
FROM {{apartments}}
WHERE agent_id != 4';
$dataProvider=new CSqlDataProvider($sql, array(
'totalItemCount'=>$count,
'sort'=>array(
'attributes'=>array(
'agent_id', //and all other atributes with withc you want to sort
),
),
'pagination'=>array(
'pageSize'=>10,
),
));
Its very simple
(SELECT *
FROM {{apartments}}
WHERE `agent_id` = '4')
UNION ALL
(SELECT *
FROM {{apartments}}
WHERE `agent_id` <> '4')
Let me know if that worked for you..
How would i convert below query to sub query?
I don't want to use JOINS I want to do same through sub query. i-e I need three subqueries out of the three joins. How it would be possible for below query ?
protected $_name = 'sale_package_features';
public function getAllSalePackageFeatures(){
$sql = $this->select()->setIntegrityCheck(false)
->from(array('spf' => $this->_name))
->joinLeft(array('sd' => 'sale_devices'),'sd.sale_device_id = spf.sale_device_id',array('sd.sale_device_name AS deviceName'))
->joinLeft(array('sp' => 'sale_packages'),'sp.sale_package_id = spf.sale_package_id',array('sp.sale_package_name AS packageName'))
->joinLeft(array('sf' => 'sale_features'),'sf.sale_feature_id = spf.sale_feature_id',array('sf.sale_feature_name AS featureName'))
->where('sf.parent_id != ?',0)
->order('spf.sale_package_feature_id ASC');
return $sql->query()->fetchAll();
}
Edited :
SELECT
`spf`.*, `sd`.`sale_device_name` AS `deviceName`,
`sp`.`sale_package_name` AS `packageName`,
`sf`.`sale_feature_name` AS `featureName`
FROM `sale_package_features` AS `spf`
LEFT JOIN `sale_devices` AS `sd`
ON sd.sale_device_id = spf.sale_device_id
LEFT JOIN `sale_packages` AS `sp`
ON sp.sale_package_id = spf.sale_package_id
LEFT JOIN `sale_features` AS `sf`
ON sf.sale_feature_id = spf.sale_feature_id
WHERE (sf.parent_id != 0)
ORDER BY `spf`.`sale_package_feature_id` ASC
You can use
$dbAdapter = Zend_Db_Table::getDefaultAdapter();
$subSelect = $dbAdapter
->select()
->from( 'tablename',
array(
'col1_alias' => 'column1_name',
'col2_alias' => 'column2_name',
))
->where('somefield = ?', $value_to_be_quoted);
$select = $dbAdapter
->select()
->from(array('sub_alias' => $subSelect ))
->where('otherfield = ?', $other_value_to_be_quoted);
$sql = $select->assemble();
$sql will contain
SELECT
sub_alias . *
FROM
(SELECT
tablename.column1_name AS col1_alias,
tablename.column2_name AS col2_alias
FROM
tablename
WHERE
(somefield = 'quoted_value')) AS sub_alias
WHERE
(otherfield = 'quoted_other_value')
If you need to use SELECT .. WHERE x IN (subselect) than go with
$select->where( 'column IN (?)', new Zend_Db_Expr($subselect->assemble()) );
Hi I have my query in codeigniter since I I have switch to Zend Framework, I wanted do my query in zend framework:
Here is my sample query:
$sql = "SELECT Mes.fromid, Mes.is_read AS is_read, Mes.id AS mesid, Mes.message AS message,
max(Mes.date) AS date, User.username AS username, User.id AS Uid
FROM `messages` AS Mes
LEFT JOIN users AS User ON User.id = Mes.fromid
WHERE Mes.toid = ? AND Mes.fromid <> ?
GROUP BY Mes.fromid ORDER BY date DESC";
$query = $this->db->query($sql, array($fromid, $fromid));
return $query->result();
Thank you for you help.
$sql = $db ->select()
->from('messages as mes',array('fromid','is_read','id as mesid','message','max(date) as date'))
->join('users as user','user.id = mes.fromid',array('username','id as uid'))
->where('mes.toid = ? ', $toid)
->where('mes.fromid = ?', $fromid)
->group('mes.fromid')
->order('date DESC');
$select = $this->select()
->setIntegrityCheck(false)
->from(
array('Mes' => 'messages'),
array('fromid', 'is_read AS is_read', 'id AS mesid', 'message AS message',
'max(Mes.date) AS date')
)
->joinLeft(array('User' => 'users'), "User.id = Mes.fromid", array('username AS username', 'id AS Uid'))
->where('mes.toid = ? ', $toid)
->where('mes.fromid = ?', $fromid)
->group('mes.fromid')
->order('date DESC');
Hope This helps :)
Check out Zend_Db_Statement, it looks like you'll have to make very few edits to make this work.
Probably just change out the Db adapter to a Zend adapter and it'll probably work.