I want to get data from db using limit 12,20 .
Here is my code:
$Query = new Query;
$Query->select(['um.id as USERid', 'um.first_name', 'um.last_name', 'um.email', 'COUNT(g.id) as guestCount'])
->from('user_master um')
->join('LEFT JOIN', 'guest g', 'g.user_id = um.id')
->limit(12,20)
->groupBy('um.id')
->orderBy(['um.id' => SORT_DESC]);
$command = $Query->createCommand();
$evevtsUserDetail = $command->queryAll();
It is not working. It is giving me all rows. I also tried ->limit([12,20]), not working.
But when I am using limit(12) then I am getting 12 rows.
I want to get rows in limit 12,20 . What should I have to do for that in my this code?
Try this:
$Query = new Query;
$Query->select(['um.id as USERid', 'um.first_name', 'um.last_name','um.email','COUNT(g.id) as guestCount'])
->from('user_master um')
->join('LEFT JOIN', 'guest g', 'g.user_id = um.id')
->limit(20)
->offset(12)
->groupBy('um.id')
->orderBy(['um.id' => SORT_DESC]);
Offset() specifies the starting point and limit() specifies the Number of records. If you want records between 12 and 20 then use limit(8).
For More Info:
http://www.bsourcecode.com/yiiframework2/select-query-model/#offset
http://www.yiiframework.com/doc-2.0/yii-db-querytrait.html#offset%28%29-detail
you can do with Active record
$model = YourModel::find()->where('user_id = :user_id', [':user_id' => \Yii::$app->user->id])->limit(12,20)->all();
OR
$model = YourModel::find()->where('user_id = :user_id', [':user_id' => \Yii::$app->user->id])->with(['job','job.jobRecipient'])->limit(12)->all();
Related
I am using below query to get results of 4 id's, but it shows only one result
$id = array(6,7,8,9);
$params = array(
'connection' => 'redis',
'id' => implode(',',$id)
);
$result = DB::select(
DB::raw("
SELECT
*
FROM
failed_jobs
WHERE
id IN (:id) AND
connection = :connection
"), $params);
The best way to go about it is to use the query builder instead. This is much cleaner since it provides the whereIn clause:
$id = array(6,7,8,9);
$result = DB::table('failed_jobs')
->where('connection', 'redis')
->whereIn('id', $id)
->get();
Following is the SQL query that I need to perform on laravel eloquent. SQL returns expected output.
SELECT
orders.id,
orders.`status`,
order_type,
COUNT(order_type) as count
FROM
orders
WHERE
orders.`status` = 0 && order_type = 1
ORDER BY
orders.id DESC
what I have tried on laravel is below
$receved = Order::select('status', 'order_type')->where('status',0);
$relase = $receved->where('order_type', 1)->get();
$bulk = $receved->where('order_type', 2)->get();
$distiribute = $receved->where('order_type', 3)->get();
return response()->json([
'success' => true,
'message' => 'Statement Updated',
'orderStatment' => [
'relaseCount' => count($relase),
'bulkCount' => count($bulk),
'distiributeCount' => count($distiribute)
],
], 200);
I seeking recommendation/suggestion to operate this in a correct way
The output I getting on laravel is
'orderStatment' => [
'relaseCount' => 14,
'bulkCount' => 0,
'distiributeCount' => 0
],
the output of expectation and SQL produce is
'orderStatment' => [
'relaseCount' => 14,
'bulkCount' => 5,
'distiributeCount' => 4
],
There are 8 Different type of status and 3 different types of order_type available on Table I want to get each order_type count of every status
You might have better luck doing it all in one query, then getting the data back out.
$receved = Order::select('status', 'order_type', DB::raw('COUNT(id) as order_count'))->where('status',0)
->groupBy('order_type')
->get();
This will give you a collection of all of the order types and their counts in one query. After that, you can get the data back out.
$bulk = $relase = $distiribute = 0;
foreach($receved as $rec) {
if($rec->order_type == 1) $relase = $rec->order_count;
elseif($rec->order_type == 2) $bulk = $rec->order_count;
elseif($rec->order_type == 3) $distiribute = $rec->order_count;
}
The problem you're facing is due to the fact that all of the following statements are manipulating the same query builder object:
$receved = Order::select('status', 'order_type')->where('status',0);
$relase = $receved->where('order_type', 1)->get();
$bulk = $receved->where('order_type', 2)->get();
$distiribute = $receved->where('order_type', 3)->get();
So the actual queries created will be something like this:
All start with: select status, order_type from orders where status = 0 and
order_type = 1;
order_type = 1 and order_type = 2;
order_type = 1 and order_type = 2 and order_type = 3;
This is why the last two queries return 0. It's expected once you see the resulting query.
You can verify this by logging the query (see this answer for details, or the docs here).
$receved is actually getting the where clauses attached to it each time. So you're not just starting with the original statement, but building onto it each time you call where.
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.
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()) );
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);