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();
Related
I will try below query, but not sure is prevent sql injection?
$status = [1, 2, 3];
$param = implode(', ', $status);
$rows = (new \yii\db\Query())
->select('*')
->from('user')
->leftJoin('post', "post.user_id = user.id AND post.some_column = $value AND post.status IN ($param)");
->all();
return expected results but may be occur sql injection. My IN condition look like is IN (1, 2, 3)
$rows = (new \yii\db\Query())
->select('*')
->from('user')
->leftJoin('post', "post.user_id = user.id AND post.some_column = :sid AND post.status IN (:param)", [':param' => $param, ':sid' => $value]);
->all();
only compare first element in array because is look like this IN ('1, 2, 3') its consist single string not check second element in array only work on first element.
I refer below link but no idea for how to implement this condition.
Can I bind an array to an IN() condition?
Please give the solution for how to use IN() Condition in On part of join(PDO/Yii2/mysql).
Based on this issue:
$rows = (new \yii\db\Query())
->select('*')
->from('user')
->leftJoin('post', ['post.user_id' => new \yii\db\Expression('user.id'), 'post.some_column' => $sid, 'post.status' => $statuesArray]);
->all();
Yii2 can create a parametrized IN condition by passing the condition as an array i.e:
['post.status' => $status]
However, converting your join condition to the array format will not work as explained in the Yii guide:
Note that the array format of where() is designed to match columns to values instead of columns to columns, so the following would not work as expected: ['post.author_id' => 'user.id'], it would match the post.author_id column value against the string 'user.id'. It is recommended to use the string syntax here which is more suited for a join:
'post.author_id = user.id'
Since you are using an INNER JOIN the result of putting the join condition in WHERE instead of in ON will be syntactically equal as explained in INNER JOIN condition in WHERE clause or ON clause?. For readability and ease of maintenance, you can leave the comparison for the tables columns in the join condition:
$rows = (new \yii\db\Query())
->select('*')
->from('user')
->innerJoin('post', 'post.user_id = user.id')
->where(['post.some_column' => $value, 'post.status' => $status])
->all();
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();
How can I write query like this In Zend Framework and fetch all the rows
SELECT * FROM tbl
WHERE user_id = $part_mail
OR user_id ='$id3';
I used to try this :
$select = $tigaseModel->fetchAll($tigaseModel
->select()
->where('user_id = ?', $part_mail )
-> orwhere('user_id = ?', $id3 ));
Following ways should help you.
Solution 1:
where( "user_id = '$part_mail' OR user_id = '$id3'" );
Solution 2:
$list = array( $part_mail, $id3 );
...
where( 'user_id in ( ? )', $list );
Solution 3:
$list = array( $part_mail, $id3 );
...
where( array( 'user_id' => $list ) );
Refer to Documentation:
Example #17 Example of an array parameter in the where() method
-You can pass an array as the second parameter to the where() method when using the SQL IN operator.
orwhere available in Zend Framework 1. But there is a space in your code before orwhere(). Check the code below:
$select = $tigaseModel->fetchAll($tigaseModel
->select()
->where('user_id = ?', $part_mail )
->orwhere('user_id = ?', $id3 ));
I want to sort multiple columns in Laravel 4 by using the method orderBy() in Laravel Eloquent. The query will be generated using Eloquent like this:
SELECT *
FROM mytable
ORDER BY
coloumn1 DESC, coloumn2 ASC
How can I do this?
Simply invoke orderBy() as many times as you need it. For instance:
User::orderBy('name', 'DESC')
->orderBy('email', 'ASC')
->get();
Produces the following query:
SELECT * FROM `users` ORDER BY `name` DESC, `email` ASC
You can do as #rmobis has specified in his answer, [Adding something more into it]
Using order by twice:
MyTable::orderBy('coloumn1', 'DESC')
->orderBy('coloumn2', 'ASC')
->get();
and the second way to do it is,
Using raw order by:
MyTable::orderByRaw("coloumn1 DESC, coloumn2 ASC");
->get();
Both will produce same query as follow,
SELECT * FROM `my_tables` ORDER BY `coloumn1` DESC, `coloumn2` ASC
As #rmobis specified in comment of first answer you can pass like an array to order by column like this,
$myTable->orders = array(
array('column' => 'coloumn1', 'direction' => 'desc'),
array('column' => 'coloumn2', 'direction' => 'asc')
);
one more way to do it is iterate in loop,
$query = DB::table('my_tables');
foreach ($request->get('order_by_columns') as $column => $direction) {
$query->orderBy($column, $direction);
}
$results = $query->get();
Hope it helps :)
Use order by like this:
return User::orderBy('name', 'DESC')
->orderBy('surname', 'DESC')
->orderBy('email', 'DESC')
...
->get();
Here's another dodge that I came up with for my base repository class where I needed to order by an arbitrary number of columns:
public function findAll(array $where = [], array $with = [], array $orderBy = [], int $limit = 10)
{
$result = $this->model->with($with);
$dataSet = $result->where($where)
// Conditionally use $orderBy if not empty
->when(!empty($orderBy), function ($query) use ($orderBy) {
// Break $orderBy into pairs
$pairs = array_chunk($orderBy, 2);
// Iterate over the pairs
foreach ($pairs as $pair) {
// Use the 'splat' to turn the pair into two arguments
$query->orderBy(...$pair);
}
})
->paginate($limit)
->appends(Input::except('page'));
return $dataSet;
}
Now, you can make your call like this:
$allUsers = $userRepository->findAll([], [], ['name', 'DESC', 'email', 'ASC'], 100);
$this->data['user_posts'] = User_posts::with(['likes', 'comments' => function($query) { $query->orderBy('created_at', 'DESC'); }])->where('status', 1)->orderBy('created_at', 'DESC')->get();
How do I filter based on a joined table's columns in Propel?
Like:
$results = FooQuery::create()->joinBar()->filterByBarSurname('surname');
You have to use use method as described in the doc:
$results = FooQuery::create()
->useBarQuery()
->filterBySurname('surname')
->endUse()
->find();
// example Query generated for a MySQL database
$query = 'SELECT foo.* from foo
INNER JOIN bar ON foo.BAR_ID = bar.ID
WHERE bar.SURNAME = :p1'; // :p1 => 'surname'
If you have to use join(), I don't think you can use filterByXXX method but the old where:
$results = FooQuery::create()
->join('Foo.Bar')
->where('Bar.surname = ?', 'surname')
->find();