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');
Related
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.
PHP:
$this->db->select('employee.*, area.Name as Area, city.Name as City, state.Name as State, group_concat(DISTINCT employee_image.Image) as Image');
$this->db->from('employee');
$this->db->join('area', 'area.ID = employee.Area', 'LEFT');
$this->db->join('city', 'city.ID = employee.City', 'LEFT');
$this->db->join('state', 'state.ID = employee.State', 'LEFT');
$this->db->join('employee_image', 'employee_image.Employee = employee.ID', 'LEFT');
$this->db->group_by('employee.ID');
return $this->db->get()->result_array();
I want to join table itself. There is column Created_by there given the value of employee id now I want to join itself and want to get first and the last name from employee table anyone can please help me How can I do it?
You can do something like
$this->db->select('e.*, a.Name as Area, c.Name as City, s.Name as State, group_concat(DISTINCT i.Image) as Image, e1.first_name createby_first_name , e1.last_name createdby_last_name ');
$this->db->from('employee e');
$this->db->join('employee as e1', 'e.Created_by = e1.ID', 'LEFT');
$this->db->join('area a', 'a.ID = e.Area', 'LEFT');
$this->db->join('city c', 'c.ID = e.City', 'LEFT');
$this->db->join('state s', 's.ID = e.State', 'LEFT');
$this->db->join('employee_image i', 'i.Employee = e.ID', 'LEFT');
$this->db->group_by('e.ID');
I tried to do sub-query SELECT in LEFT JOIN with an Active Records CI, but it gives an error:
near 'b.MedicalFacilitiesIdUser FROM medicalfacilities AS b WHERE b.MedicalFacilitiesI'
How as look, the query was distorted: b.MedicalFacilitiesI
Query is:
$this->db->join('stocks AS stn', 'stn.stocksIdMF =
(SELECT b.MedicalFacilitiesIdUser FROM medicalfacilities AS b
WHERE b.MedicalFacilitiesIdUser = stn.stocksIdMF AND stn.stocksEndDate >= UNIX_TIMESTAMP()
ORDER BY stn.stocksId DESC LIMIT 1)', 'LEFT');
Now full query is:
$this->db->select("MedicalFacilitiesName, MedicalFacilitiesAdres, MedicalFacilitiesDescription,
MedicalFacilitiesView, medicalfacilities.country, medicalfacilities.city, MedicalFacilitiesPhoto,
MedicalFacilitiesIdUser, idMedicalFacilities, id_specialization, id_MF, idUsers, UsersTypeAccount,
COUNT(commentstomedicalfacilities.idCommentsToMedicalFacilities) AS CNT,
COUNT(DISTINCT(stocks.stocksId)) AS count_stocks,
COUNT( su.idSubscrubeToUsers ) AS SBS,
coalesce(ROUND((SUM(ev.evaluationinstitutionEvalTotal) / SUM(ev.evaluationinstitutionEvalTotalRate))), 0) AS rate,
stocks.stocksName, stocks.stocksId, stocks.stocksEndDate, stocks.stocksStartDate,
typesmedicalfacilities.name AS nameType", FALSE);
$this->db->from('medicalfacilities');
$this->db->join('users', 'users.idUsers = medicalfacilities.MedicalFacilitiesIdUser');
$this->db->join('medicalfacilities_directions', 'medicalfacilities_directions.id_MF = medicalfacilities.MedicalFacilitiesIdUser', 'LEFT');
$this->db->join('subscrubetousers su', 'su.SubscrubeToUsersIdNote = medicalfacilities.MedicalFacilitiesIdUser AND su.SubscrubeToUsersType = 9', 'LEFT');
$this->db->join('thematicspecialization', 'thematicspecialization.idSpecialization = medicalfacilities_directions.id_specialization', 'LEFT');
$this->db->join('evaluationinstitution ev', 'ev.evaluationinstitutionIdInst = medicalfacilities.MedicalFacilitiesIdUser', 'LEFT');
$this->db->join('stocks', 'stocks.stocksIdMF = medicalfacilities.MedicalFacilitiesIdUser AND stocks.stocksEndDate >= UNIX_TIMESTAMP()', 'LEFT');
$this->db->join('commentstomedicalfacilities', 'commentstomedicalfacilities.CommentsToMedicalFacilitiesIdMedical = medicalfacilities.MedicalFacilitiesIdUser', 'LEFT');
$this->db->join('medicalfacilities_type', 'medicalfacilities_type.idMF = medicalfacilities.MedicalFacilitiesIdUser');
$this->db->join('typesmedicalfacilities', 'typesmedicalfacilities.type = medicalfacilities_type.type');
$this->db->join('stocks AS stn', 'stn.stocksIdMF = (SELECT b.MedicalFacilitiesIdUser FROM medicalfacilities AS b WHERE b.MedicalFacilitiesIdUser = stn.stocksIdMF AND stn.stocksEndDate >= UNIX_TIMESTAMP()
ORDER BY stn.stocksId DESC LIMIT 1)', 'LEFT');
Can anybody tell me why im not able to display the following query using active record in codeigniter. I suppose there is something wrong with TIMEDIFF in my select statement as theres no space between TOTAL and FROM
This is my active record query
$this->db->select('s.id, s.person_id, p.first_name, p.last_name, sch.id, sch.start_date, sch.start_hour, sch.end_hour, TIMEDIFF(sch.end_hour, sch.start_hour) AS total');
$this->db->join('staff s', 'sch.staff_id = s.id', 'left');
$this->db->join('persons p', 's.person_id = p.id', 'left');
$this->db->where('sch.start_date >=', $start);
$this->db->where('sch.start_date <=', $end);
$this->db->limit(10);
$query = $this->db->get('work_schedule sch');
And this is how is printed out
SELECT `s`.`id`,
`s`.`person_id`,
`p`.`first_name`,
`p`.`last_name`,
`sch`.`id`,
`sch`.`start_date`,
`sch`.`start_hour`,
`sch`.`end_hour`,
TIMEDIFF(sch.end_hour,`sch`.`start_hour)` AS totalFROM (`work_schedule` sch)
LEFT JOIN `staff` s ON `sch`.`staff_id` = `s`.`id`
LEFT JOIN `persons` p ON `s`.`person_id` = `p`.`id`
WHERE `sch`.`start_date` >= '2014-02-19'
AND `sch`.`start_date` <= '2014-02-20'LIMIT 10
You can try it.
$query = $this->db->select('s.id, s.person_id, p.first_name, p.last_name, sch.id, sch.start_date, sch.start_hour, sch.end_hour, TIMEDIFF(sch.end_hour, sch.start_hour) AS total', FALSE)
->from('work_schedule sch')
->join('staff s', 'sch.staff_id = s.id', 'left')
->join('persons p', 's.person_id = p.id', 'left')
->where('sch.start_date >=', $start)
->where('sch.start_date <=', $end)
->limit(10)
->get();
$result = $query->result();
I am facing a problem when using active record or_where
how i do or only for one particular field i.e meeting_status
$this->db->_protect_identifiers=false;
$this->db->select("g.category_id,f.doc_type_id,g.category_name,f.doc_type_name,c.meeting_name_id, c.meeting_name as mn, d.meeting_type_id, d.meeting_type_name, e.venue_id, e.venue_name, u.*, (select count(*) from tbl_meeting_decisions where meeting_id=u.meeting_id and record_status= 'ACTIVE') as `meeting_decision`, (select count(*) from tbl_meeting_actions where meeting_id=u.meeting_id and record_status= 'ACTIVE') as `meeting_action`, (select count(*) from tbl_meeting_actions where meeting_id=u.meeting_id and action_status='OPEN' and record_status= 'ACTIVE') as `meeting_close_action`");
$this->db->from('tbl_meeting_plans u');
$this->db->join('tbl_meeting_names c', 'c.meeting_name_id = u.meeting_name');
$this->db->join('tbl_meeting_types d', 'd.meeting_type_id = u.meeting_mode_id');
$this->db->join('tbl_meeting_venue e', 'e.venue_id = u.venue_id ');
$this->db->join('tbl_document_types f', 'f.doc_type_id = u.meeting_type_id ');
$this->db->join('tbl_categories g', 'g.category_id = u.meeting_category_id ');
$this->db->where(array('u.meeting_type_id'=>$str2,'u.meeting_category_id'=>$str1,'u.meeting_status'=>'Planning','u.record_status'=>'ACTIVE' ));
$this->db->or_where(array('u.meesting_status'=>'Meeting Updated' ));
$where = "(u.meeting_status='Planning' or u.meeting_status='Meeting
Updated') ";
$this->db->where($where);
Before this use the normal where clause