how to Convert this query to linq of codeigniter - codeigniter

I don't understand how to convert this query into codeigniter linq
SELECT prdctSubCatId
FROM productinfo
WHERE prdctSubCatId = (
SELECT prdctSubCatId
FROM productinfo
WHERE id='27'
)
Instead of id ='27' I want to pass id=$id, then how can I do this

$this->db->select('prdctSubCatId')->from('productinfo');
$this->db->where('`prdctSubCatId`= (SELECT `prdctSubCatId` FROM `productinfo` WHERE id=27 )', NULL, FALSE);

$this->db->select('prdctSubCatId');
$this->db->where('id',27);
return $this->db->get('productinfo')->result_array();

Simply use
$query = $this->db->query("SELECT prdctSubCatId FROM productinfo WHERE prdctSubCatId=27");
$result = $query->result_array();
return $result;

Related

AND, OR use In Join query in Codeigniter

How to use Below query in Codeigniter
SELECT * FROM (post) JOIN user ON user.user_id=post.user_id JOIN friend ON (friend.user_id=post.user_id AND friend.friendship_id=1) OR (friend.user_id=1 ANDfriend.friendship_id=post.user_id) WHERE post.type = 'friend' ORDER BY postid desc
You can use this.
$this->db->query(your query);
like this code.
$sql = "SELECT * FROM table WHERE user_id = 1";
$this->db->query($sql);
try this
$query = $this->db
->select('*')
->from('post')
->join('user', 'user.user_id = post.user_id')
->join('friend', 'friend.user_id = (post.user_id AND friend.friendship_id=1) OR (friend.user_id = 1 AND friend.friendship_id = post.user_id)')
->where('post.type', 'friend')
->order_by('postid','DESC')
->get();
$arrResult = $query->result();

Is there a way to display the value instead of (?) using query builder in laravel eloquent?

I'm using Eloquent Query Builder to produce raw sql for me. My goal is to return a sql query like this
select * from accounts where id = '120'
now i tried
$query = \App\Account::query();
$query = $query->where('id', 120);
dd($query->toSql());
Output:
select * from `accounts` where `id` = ?
is there a way to display the value instead of (?) because this is not a valid query on the third party API that i'm using.
I'm expecting to produce a query on my first example that i give.
Note: I'm not querying on my database, i will use that raw query to pass on the 3rd party API. All i want is to produce only a raw sql. Any suggestion would be appreciated. Thanks!
Yes, you can print the parameters value by using getBindings()
print_r( $query->getBindings() );
Array
(
[0] => instructor
[1] => 1
)
Found a solution for this, maybe it can help to someone that has same problem with me.
You can check this link: https://gist.github.com/JesseObrien/7418983
public function getSql()
{
$builder = $this->getBuilder();
$sql = $builder->toSql();
foreach($builder->getBindings() as $binding)
{
$value = is_numeric($binding) ? $binding : "'".$binding."'";
$sql = preg_replace('/\?/', $value, $sql, 1);
}
return $sql;
}

Laravel pagination not working with group by clause

It seems Laravel pagination does not working properly with group by clause. For example:
$users = Subject::select(DB::raw('subjects.*, count(user_subjects.id) as total_users'))
->join('user_subjects', 'user_subjects.subject_id', '=', 'subjects.id')
->whereNull('user_subjects.deleted_at')
->groupBy('subjects.id')
->orderBy('subjects.updated_at', 'desc')
->paginate(25);
Produced
select subjects.*, count(user_subjects.id) as total_users
from `subjects` inner join `user_subjects` on `user_subjects`.`subject_id` = `subjects`.`id`
where `subjects`.`deleted_at` is null and `user_subjects`.`deleted_at` is null
group by `subjects`.`id`
order by `subjects`.`updated_at` desc
note that, there is no limit clause on the query.
Working fine if no group by clause in the query:
$users = Subject::select(DB::raw('subjects.*, count(user_subjects.id) as total_users'))
->join('user_subjects', 'user_subjects.subject_id', '=', 'subjects.id')
->whereNull('user_subjects.deleted_at')
->orderBy('subjects.updated_at', 'desc')
->paginate(25);
produced the following query:
select subjects.*, count(user_subjects.id) as total_users from `subjects`
inner join `user_subjects` on `user_subjects`.`subject_id` = `subjects`.`id`
where `subjects`.`deleted_at` is null and `user_subjects`.`deleted_at` is null
order by `subjects`.`updated_at` desc
limit 25 offset 0
does anyone has any idea how can i fix this?
Check the documentation
https://laravel.com/docs/5.2/pagination
Currently, pagination operations that use a groupBy statement cannot
be executed efficiently by Laravel. If you need to use a groupBy with
a paginated result set, it is recommended that you query the database
and create a paginator manually.
I know it is an old question, by I am sharing my solution for future reference.
I managed to write a function based on this link which does the heavy job of determining the pagination of a complex query. Just pass the 'QueryBuilder' and it will return the paginated object/collection.
Additionally, this procedure can track and maintain the other parameters except for page=.
public function mergeQueryPaginate(\Illuminate\Database\Eloquent\Builder $query): \Illuminate\Pagination\LengthAwarePaginator
{
$raw_query = $query;
$totalCount = $raw_query->get()->count();
$perPage = request('per-page', 10);
$page = request('page', 1);
$skip = $perPage * ($page - 1);
$raw_query = $raw_query->take($perPage)->skip($skip);
$parameters = request()->getQueryString();
$parameters = preg_replace('/&page(=[^&]*)?|^page(=[^&]*)?&?/', '', $parameters);
$path = url(request()->getPathInfo() . '?' . $parameters);
$rows = $raw_query->get();
$paginator = new LengthAwarePaginator($rows, $totalCount, $perPage, $page);
$paginator = $paginator->withPath($path);
return $paginator;
}
This works for me in laravel 5.2
Select(\DB::RAW("assignment_descendant_child.assignment_descendant_child_id, assignment_descendant_child.assignment_descendant_child_name, COUNT(assignment_descendant.assignment_descendant_id) as xNum"))
->leftJoin(
'assignment_descendant',
'assignment_descendant.assignment_descendant_child_id',
'=',
'assignment_descendant_child.assignment_descendant_child_id'
)
->orderBy('assignment_descendant_child_name')
->groupBy('assignment_descendant_child.assignment_descendant_child_id')
->paginate(\Config::get('constants.paginate_org_index'))
create a database view namedvw_anything. MySql query will be like
create view vw_anything as select subjects.*, count(user_subjects.id) as total_users from subjects inner join user_subjects on user_subjects.subject_id = subjects.id
where subjects.deleted_at is null and user_subjects.deleted_at is null group by subjects.id;
Now create a new model named UserSubModel for this view, protected $table = 'vw_anything';
Now your paginate query will be like UserSubModel::orderBy('subjects.updated_at', 'desc')->paginate(25);
.
To answer this questioin Laravel Pagination group by year and month only
View query will be :
create view vw_anything as select gallery.*, DATE_FORMAT(created_at, "%Y-%m") as tanggal,count(created_at) as jumlah from gallery group by tanggal;
Let you model is VwModel then your paginate query will be
VwModel::where('type','Foto')->orderBy('tanggal','desc')->paginate(2);
This works if you want to group by and paginate.
$code = DB::table('sources')
->select(DB::raw('sources.id_code,sources.title,avg(point) point'))
->join('rating','sources.id_code','rating.id_code')
->groupBy('sources.id_code')
->groupBy('sources.title')
->groupBy('sources.language')
->groupBy('sources.visited')
->paginate(5);

Get the result of SUM and group field from Active record in Codeigniter

I need to build this query in Codeigniter but I don't know how to get the result of the SUM:
SELECT
description, SUM(amount)
FROM
PAYMENT
WHERE
(date_payment between '2014-02-01 00:00:00' AND '2014-02-28 23:59:59')
GROUP BY description;
I'm trying to get the result with this:
$qwer = $this->db->query("
SELECT description, SUM(amount)
FROM PAYMENT
WHERE (date_payment between '$min_date' AND '$max_date')
GROUP BY description;");
$i = 0;
foreach ($qwer->result() as $payment) {
$det_payment[$i] = array(
'description'=>$payment->description,
'amount'=>$payment->amount
);
$i++;
}
Of course "$payment->amount" is wrong, but if I use an alias for the SUM, the model doesn't work.
EDIT: Right now I can choose between the description or the sum, but I can't use both select and select_sum
$this->db->select('description');
//$this->db->select_sum('amount', 'amount');
$this->db->where('date_payment >=', $min_fecha);
$this->db->where('date_payment <=', $max_fecha);
$this->db->group_by("description");
$qwer = $this->db->get('PAYMENT');
Setting FALSE as second parameter, 'select' allows to write a custom sentence.
$this->db->select('description, SUM(amount) AS amount', FALSE);
$this->db->where('date_payment >=', $min_date);
$this->db->where('date_payment <=', $max_date);
$this->db->group_by("description");
$qwer = $this->db->get('PAYMENT');
Documentation for CI2
Documentation for CI3
try something like this:
$query = $this->db->select_sum('amount', 'Amount');
$query = $this->db->where(...)
$query = $this->db->get('payment');
$result = $query->result();
return $result[0]->Amount;

codeigniter active record get last 10 records and sort ASC

I would like to know if this SQL statement is done in Codeigniter active record.
SELECT * FROM (
SELECT *
FROM chat
WHERE (userID = $session AND toID = $friendID)
OR (userID = $friendID AND toID = $session)
ORDER BY id DESC
LIMIT 10
) AS `table` ORDER by id ASC
You have to use the _compile_select() and _reset_select() methods of the active record class.
$this->db->select('*');
$this->db->from('chat');
$this->db->where("(userID='{$session}' AND toID='{$friendID}')");
$this->db->or_where("(userID='{$friendID}' AND toID='{$session}')");
$this->db->order_by('id', 'DESC');
$this->db->limit('10');
$subQuery = $this->db->_compile_select();
$this->db->_reset_select();
$this->db->select('*');
$this->db->from("{$subQuery} AS table");
$this->db->order_by('id', 'ASC');
$query = $this->db->get();
Unfortunately, in CI 2.0+, _compile_select() and _reset_select() are protected methods. Bummer. You'll have to follow this tutorial on extending the DB driver where you can write methods like the following:
function get_compiled_select()
{
return $this->db->_compile_select();
}
function do_reset_select()
{
$this->db->_reset_select();
}
I would like to take the time to point out that this type of action would be better served by joins. You should consider changing your db structure so that joins are possible and efficient.
This will work, though is not the active record variant.
$this->db->query( "SELECT * FROM (
SELECT *
FROM chat
WHERE (userID = ? AND toID = ?)
OR (userID = ? AND toID = ?)
ORDER BY id DESC
LIMIT 10
) AS `table` ORDER by id ASC", array( $session, $friendID, $friendID, $session) );"
You can use query as:
$this->db->select('SELECT *
FROM chat
WHERE (userID = $session AND toID = $friendID)
OR (userID = $friendID AND toID = $session)
ORDER BY id DESC
LIMIT 10') AS `table` ORDER by id ASC', FALSE);

Resources