subquery treated as char in subquery codeigniter - php

I tried to use subquery in codeigniter to get the result and use the result as the parameter in where in clause. but i got this error.
and here is my codeigniter active record query:
$session['hasil'] = $this->session->userdata('logged_in');
$regional = $session['hasil']->regional;
$this->db->select('a.id_agency');
$this->db->from('tbl_agency a');
$this->db->join('tbl_collector_agency b', 'a.id_agency = b.id_agency', 'left');
$this->db->join('tbl_area_collector c', 'b.id_collector = c.id_collector', 'left');
$this->db->join('mysystem.lapkeu.dbo.groupbranch d', 'c.group_branch_id = d.GroupBranchID', 'left');
$this->db->where('d.regional', $regional);
$subQuery = $this->db->get_compiled_select();
$this->db->reset_query();
$this->db->select('*');
$this->db->from($this->table);
$this->db->where_in('id_agency', $subQuery);
im using microsoft sql server as my database.

Related

How do I call the BETWEEN function in php?

I am trying to filter by date and status, in mysql the query is fine, but when implementing php it does not work for me.
My Consult Sql
SELECT `loan_items`.*
FROM `loan_items`
WHERE `date` BETWEEN '2023-03-10' AND '2023-03-10' AND `status` = 1;
Code Php
$date = date("y-d-m");
$this->db->select("li.id, c.dni, concat(c.first_name,' ',c.last_name) AS name_cst, l.id AS loan_id, li.pay_date, li.num_quota, li.fee_amount");
$this->db->from('loan_items li');
$this->db->join('loans l', 'l.id = li.loan_id', 'left');
$this->db->join('customers c', 'c.id = l.customer_id', 'left');
$this->db->where(['li.status' => 1, 'li.date' => "BETWEEN :$date AND :$date"]);
$this->db->order_by('li.pay_date', 'desc');
This way it works but it doesn't show me the date
$this->db->select("li.id, c.dni, concat(c.first_name,' ',c.last_name) AS name_cst, l.id AS loan_id, li.pay_date, li.num_quota, li.fee_amount");
$this->db->from('loan_items li');
$this->db->join('loans l', 'l.id = li.loan_id', 'left');
$this->db->join('customers c', 'c.id = l.customer_id', 'left');
$this->db->where('li.status', 1);
$this->db->order_by('li.pay_date', 'desc');

Order by not working with Group by in codeigniter

$loginuserID = $this->session->userdata("loginuserID");
$this->db->select('notice_x_user.*,notice.*,classes.*,sub_courses.*,,notice.status as studentStatus,notice.date as noticeDate');
$this->db->from('notice_x_user');
$this->db->join('notice','notice_x_user.noticeID = notice.noticeID', 'LEFT');
$this->db->join('classes', 'classes.ClassesID = notice.classesID', 'LEFT');
$this->db->join('sub_courses', 'sub_courses.sub_coursesID = notice.sub_coursesID', 'LEFT');
$wheres = "(notice_x_user.userID = '".$loginuserID."' and notice_x_user.usertype = 'Support') or notice.userID = '".$loginuserID."'";
$this->db->where($wheres);
$this->db->order_by('notice.noticeID', 'DESC');
$this->db->group_by('notice.noticeID');
I am join four table where I get correct data but the problem is that order by is not working while using group by. So, How can I do this? Please help me.
Thank You
use select_max for getting the max noticeID
$this->db->select('notice_x_user.*,notice.*,classes.*,sub_courses.*,,notice.status as studentStatus,notice.date as noticeDate');
$this->db->select_max('notice.noticeID' , 'noticeID');
$this->db->from('notice_x_user');
$this->db->join('notice','notice_x_user.noticeID = notice.noticeID', 'LEFT');
$this->db->join('classes', 'classes.ClassesID = notice.classesID', 'LEFT');
$this->db->join('sub_courses', 'sub_courses.sub_coursesID = notice.sub_coursesID', 'LEFT');
$wheres = "(notice_x_user.userID = '".$loginuserID."' and notice_x_user.usertype = 'Support') or notice.userID = '".$loginuserID."'";
$this->db->where($wheres);
$this->db->order_by('noticeID', 'DESC');
$this->db->group_by('notice.noticeID');
Grouping statements must be used before ordering statements, otherwise you'll get a MySQL error.
Just put the statements in the correct order:
$this->db->group_by('notice.noticeID');
$this->db->order_by('notice.noticeID', 'DESC');
and it'll work as expected

Codeigniter query count() total comments joining three tables

I am attempting to create a query to get a single blog post from my blog table and at the same time get the information from that user on my users' table which was ok using Join but now I want to count the total comments of that blog post as total so that would be three tables to query blog, users and comments
But below code display 3 blog entry with the same content, and where to place the COUNT(*) as total for comment table, any suggestion would be great!
public function get_entry(){
$id = $this->input->post('ID', true);
$this->db->select('*, u.ID');
$this->db->where('u.ID', $id)
->from('gb_blod as u')
->join('gb_users as a', 'u.user_email = a.email', 'LEFT')
->join('gb_comments as b', 'u.ID = b.journal_id', 'LEFT');
$result = $this->db->get();
if($result->num_rows() > 0){
return $result->result_array();
}else{
return false;
}
}
Count the comment id and group the query by blog id.
public function get_entry(){
$id = $this->input->post('ID', true);
$this->db->select('u.*, a.*, count(b.ID) as total');
$this->db->where('u.ID', $id)
->from('gb_blod as u')
->join('gb_users as a', 'u.user_email = a.email', 'LEFT')
->join('gb_comments as b', 'u.ID = b.journal_id', 'LEFT')
->group_by('u.ID');
$result = $this->db->get();
if($result->num_rows() > 0){
return $result->result_array();
}else{
return false;
}
}
Try using this, you can change the query according to your requirement:-
$usr_flds = "count(u.ID) as count_rows";
$this->db->select('usr_flds');
$this->db->where('u.ID', $id)
->from('gb_blod as u')
->join('gb_users as a', 'u.user_email = a.email', 'LEFT')
->join('gb_comments as b', 'u.ID = b.journal_id', 'LEFT');
$result = $this->db->get();
return $res->num_rows();
try this.i hope it will work for you.
$result = $this->db->get();
$count = $result->get()->num_rows();
return [$result, $count];

How to write this query in codeigniter active records

How to write this query in codeigniter active records
select
a.parent_cat_id,a.parent_cat_name, b.child_cat_id,b.child_cat_name,
c.sub_child_cat_id,c.sub_child_cat_name
FROM parent_categories a,child_categories b,sub_child_categories c
WHERE a.parent_cat_id=b.parent_cat_id AND b.child_cat_id=c.child_cat_id
Tried this but it Shows 0 result
$this->db->select('a.parent_cat_id,a.parent_cat_name, b.child_cat_id,b.child_cat_name,c.sub_child_cat_id,c.sub_child_cat_name');
$this->db->from('parent_categories a,child_categories b,sub_child_categories c');
$this->db->where('a.parent_cat_id','b.parent_cat_id');
$this->db->where('b.child_cat_id','c.child_cat_id');
$result = $this->db->get()->result_array();
when i echo the above ci query i get
SELECT `a`.`parent_cat_id`, `a`.`parent_cat_name`, `b`.`child_cat_id`, `b`.`child_cat_name`, `c`.`sub_child_cat_id`, `c`.`sub_child_cat_name`
FROM `parent_categories` `a`, `child_categories` `b`, `sub_child_categories` `c`
WHERE `a`.`parent_cat_id` = 'b.parent_cat_id'
AND `b`.`child_cat_id` = 'c.child_cat_id'
Try changing $this->db->where in your query as below-
$this->db->select('a.parent_cat_id,a.parent_cat_name, b.child_cat_id,b.child_cat_name,c.sub_child_cat_id,c.sub_child_cat_name');
$this->db->from('parent_categories a,child_categories b,sub_child_categories c');
$this->db->where("a.parent_cat_id = b.parent_cat_id");
$this->db->where("b.child_cat_id = c.child_cat_id");
$result = $this->db->get()->result_array();
You have to use Join Query For That Here is Code Snippet
$this->db->select('a.parent_cat_id,a.parent_cat_name, b.child_cat_id,b.child_cat_name,c.sub_child_cat_id,c.sub_child_cat_name');
$this->db->from('parent_categories a');
$this->db->join('child_categories b', 'b.parent_cat_id = a.parent_cat_id', 'left');
$this->db->join('sub_child_categories c', 'c.child_cat_id = b.child_cat_id', 'left');
$query = $this->db->get();
$res = $query->result();
I didn't have your table structure and data to check. But, try this. It will work.
$this->db->select('a.parent_cat_id,a.parent_cat_name, b.child_cat_id,b.child_cat_name,c.sub_child_cat_id,c.sub_child_cat_name');
$this->db->from('parent_categories a,child_categories b,sub_child_categories c');
$this->db->join('a.parent_cat_id','b.parent_cat_id');
$this->db->join('b.child_cat_id','c.child_cat_id');
$this->db->get();

How to use sub query in INNER JOIN ON condition in zf2..?

I have to implement a condition in sql query where i need to use sub query in INNER JOIN ON condition as follows :
$select = new Select('booking');
$select->columns(array('*', new Expression("service_provider.first_name as sp_first_name, service_provider.last_name as sp_last_name,
invoice.status_id AS invoice_status,payment_history.currency as currency,CASE invoice.status_id WHEN 0 THEN 'Unpaid' WHEN 1 THEN 'Paid' WHEN 2 THEN 'Partially Paid' END AS PaymentStatus")));
$select->join('booking_suggestion_history', 'booking_suggestion_history.booking_id = booking.id AND booking_suggestion_history.id = (SELECT id FROM booking_suggestion_history WHERE booking_id = booking.id ORDER BY id DESC LIMIT 1)', 'inner');
$select->join('users', 'users.id = booking.user_id', array('first_name', 'last_name'), 'left');
$select->join(array('service_provider' => 'users'), 'service_provider.id = booking.service_provider_id', array(), 'left');
$select->join('service_provider_service', 'service_provider_service.id = booking.service_provider_service_id', array('duration', 'price'), 'left');
$select->join('service_category', 'service_category.id = service_provider_service.service_id', array('category_name'), 'left');
$select->join('invoice', 'invoice.id = booking.invoice_id', array('invoice_total', 'site_commision'), 'inner');
$select->join('invoice_details', 'invoice_details.invoice_id = invoice.id', array('sale_item_details'), 'inner');
$select->join('payment_history', 'payment_history.invoice_id = invoice.id', array(), 'inner');
$select->join('lookup_status', 'lookup_status.status_id = booking.status_id', array('status'), 'left');
query generating through these conditions (got through str_replace('"', '', $select->getSqlString());) is working fine in sql but zf2 throwing error :
Statement could not be executed (42000 - 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '`id` `FROM` `booking_suggestion_history` `WHERE` `booking_id` = `booking`.`id` `' at line 2)
Please help..!!

Categories