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();
Related
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');
Hai all I have this raw query in mysql I need to translate to laravel 5.4 query builder:
SELECT MONTH(calendar.datefield) AS Bulan, calendar.datefield AS Tanggal, IFNULL(SUM(dbk.jumlah),0) AS Jumlah
FROM detail_brg_keluar AS dbk
INNER JOIN penjualan AS pen ON pen.id = dbk.id_brg_keluar
RIGHT JOIN calendar ON (DATE(pen.tgl_keluar) = calendar.datefield)
WHERE
(calendar.datefield BETWEEN
(SELECT MIN(DATE(tgl_keluar)) FROM penjualan AS p
INNER JOIN detail_brg_keluar AS detail ON p.id = detail.id_brg_keluar
WHERE detail.id_produk = 818002)
AND
(SELECT MAX(DATE(tgl_keluar)) FROM penjualan AS pe
INNER JOIN detail_brg_keluar AS det ON pe.id = det.id_brg_keluar
WHERE det.id_produk = 818002))
AND (dbk.id_produk = 818002 OR dbk.id_produk IS NULL)
GROUP BY MONTH(calendar.datefield)
ORDER BY calendar.datefield ASC
This my laravel code, and code error -_-
$proT = DB::table('detail_brg_keluar AS dbk')
->select( DB::raw('MONTH(calendar.datefield) AS Bulan, calendar.datefield AS Tanggal, IFNULL(SUM(dbk.jumlah),0) AS Jumlah '))
->join('penjualan AS pen', 'pen.id', '=', 'dbk.id_brg_keluar')
->join('calendar', DB::raw('DATE(pen.tgl_keluar)'), '=', 'calendar.datefield')
->whereBetween('calendar.datefield', [$from[0]->tanggalAwal, $to[0]->tanggalAkhir])
->where(DB::raw('dbk.id_produk = 818002 OR dbk.id_produk IS NULL'))
->groupBy(DB::raw('MONTH(calendar.datefield)'))
->orderBy(DB::raw('calendar.datefield', 'ASC'))
->get();
Code from and to :
$from = DB::table('penjualan AS p')
->select(DB::raw('MIN(DATE(tgl_keluar)) AS tanggalAwal'))
->join('detail_brg_keluar AS detail', 'p.id', '=', 'detail.id_brg_keluar')
->where('detail.id_produk', $id)
->get();
$to = DB::table('penjualan AS p')
->select(DB::raw('MAX(DATE(tgl_keluar)) AS tanggalAkhir'))
->join('detail_brg_keluar AS detail', 'p.id', '=', 'detail.id_brg_keluar')
->where('detail.id_produk', $id)
->get();
I have no idea anymore?
Thx all
Use this:
$from = DB::table('penjualan AS p')
->select(DB::raw('MIN(DATE(tgl_keluar)) AS tanggalAwal'))
->join('detail_brg_keluar AS detail', 'p.id', '=', 'detail.id_brg_keluar')
->where('detail.id_produk', $id);
$to = DB::table('penjualan AS p')
->select(DB::raw('MAX(DATE(tgl_keluar)) AS tanggalAkhir'))
->join('detail_brg_keluar AS detail', 'p.id', '=', 'detail.id_brg_keluar')
->where('detail.id_produk', $id);
$proT = DB::table('detail_brg_keluar AS dbk')
->select( DB::raw('MONTH(calendar.datefield) AS Bulan, calendar.datefield AS Tanggal, IFNULL(SUM(dbk.jumlah),0) AS Jumlah '))
->join('penjualan AS pen', 'pen.id', '=', 'dbk.id_brg_keluar')
->rightJoin('calendar', DB::raw('DATE(pen.tgl_keluar)'), '=', 'calendar.datefield')
->whereRaw('calendar.datefield BETWEEN ('.$from->toSql().') AND ('.$to->toSql().')', [$id, $id])
->where(function($query) {
$query->where('dbk.id_produk', 818002)->orWhereNull('dbk.id_produk');
})
->groupBy(DB::raw('MONTH(calendar.datefield)'))
->orderBy('calendar.datefield', 'ASC')
->get();
Don't execute $from and $to, use the generated SQL.
I replaced ->join('calendar' with ->rightJoin('calendar'.
I replaced where(DB::raw()) with a nested constraint.
orderBy() doesn't need a raw expression.
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');
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
I'm trying to make a mail system in Codeigniter with the PyroCms.
In my mail table, I have a "recipent" row and a "sender" row which contains the user id of the sender and recipient. To retrieve usernames from the ids, I'm trying to join the table together, but it simply returns me this error:
Error Number: 1066
Not unique table/alias: 'default_users'
SELECT `default_mailsystem`.*, `default_users`.`username` AS modtager, `default_users`.`username` as afsender
FROM (`default_mailsystem`)
LEFT JOIN `default_users` ON `default_mailsystem`.`recipent` = `default_modtager`.`id`
LEFT JOIN `default_users` ON `default_mailsystem`.`sender` = `default_afsender`.`id`
ORDER BY `id` DESC
Filename: /hsphere/local/home/brightmedia/reuseable.dk/modules/mail/models/mail_m.php
Line Number: 13
My code is as follows:
$this->db->select('mailsystem.*, users.username AS modtager, users.username as afsender')
->join('users', 'mailsystem.recipent = modtager.id', 'left')
->join('users', 'mailsystem.sender = afsender.id', 'left');
$this->db->order_by('id', 'DESC');
return $this->db->get('mailsystem')->result();
The funny thing is, that if I remove the last "join" operation and leave it to only join the recipient of the mail it all works out well.
This is very simple
$this->db->select('mailsystem.*, users.username AS modtager, users.username as afsender')
$this->db->join('users', 'mailsystem.recipent = modtager.id AND mailsystem.sender = afsender.id', 'left')
$this->db->order_by('id', 'DESC');
return $this->db->get('mailsystem')->result();
Have you tried forcing an alias in the join function (the "AS" operator won't work in the select clause as you have it...)?
<?php
$this->db->select('mailsystem.*, modtager.username AS modtager_name, afsender.username as afsender_name')
->join('`users` `modtager`', 'mailsystem.recipent = modtager.id', 'left')
->join('`users` `afsender`', 'mailsystem.sender = afsender.id', 'left');
$this->db->order_by('mailsystem.id', 'DESC');
return $this->db->get('mailsystem')->result();
Use alias like this -
$this->db->select('mailsystem.*, users_table_a.username AS modtager, users_table_b.username as afsender')
$this->db->join('users users_table_a', 'mailsystem.recipent = users_table_a.id', 'left');
$this->db->join('users users_table_b', 'mailsystem.sender = users_table_b.id', 'left');
$this->db->order_by('id', 'DESC');
return $this->db->get('mailsystem')->result();
you can use this :
$this->db->select('mailsystem.*, users.username AS modtager, users.username as afsender')
$this->db->join('users', 'mailsystem.recipent = modtager.id AND mailsystem.sender = afsender.id', 'left')
$this->db->order_by('id', 'DESC');
return $this->db->get('mailsystem')->result();
*If you are using any db prefix use this *
$this->db->select('mailsystem.*, users.username AS modtager, users.username as afsender')
$this->db->join('users', 'mailsystem.recipent = modtager.id AND '.$this->db->dbprefix('mailsystem').'.sender = '.$this->db->dbprefix('afsender').'.id', 'left')
$this->db->order_by('id', 'DESC');
return $this->db->get('mailsystem')->result();
Its very easy to get data from single table
$this->db->select('a.*,b.fname AS cname,c.fname as uname');
$this->db->from('tbl_menus a');
$this->db->join('tbl_admin_login b', 'b.id = a.create_by', 'left');
$this->db->join('tbl_admin_login c', 'c.id = a.update_by', 'left');
$this->db->order_by('a.id', 'desc');
return $this->db->get()->result_array();
At the time of this thread, i figured that the codeigniter call missed out the possibility to do AS inside of a join, Therefor this solved the issue:
$sql = "
SELECT default_mailsystem.*,
recipent.first_name AS modtager,
sender.first_name AS afsender
FROM default_mailsystem
LEFT JOIN default_profiles AS recipent ON recipent.id = default_mailsystem.id
LEFT JOIN default_profiles AS sender ON sender.id = default_mailsystem.id
";
return $this->db->query($sql)->result();
Your can try this
Core PHP
SELECT `custome_module`.`id`, `custome_module`.`name`, min(l1.nmark_completed) as call_waiter, min(l2.nmark_completed) as bill, min(l3.nmark_completed) as tray, min(l4.nmark_completed) as ordera
FROM `custome_module`
LEFT JOIN `restaurant_logs` as `l1` ON `custome_module`.`id` = `l1`.`nmodule_id` AND `l1`.`ntype` = 1
LEFT JOIN `restaurant_logs` as `l2` ON `custome_module`.`id` = `l2`.`nmodule_id` AND `l2`.`ntype` = 2
LEFT JOIN `restaurant_logs` as `l3` ON `custome_module`.`id` = `l3`.`nmodule_id` AND `l3`.`ntype` = 6
LEFT JOIN `restaurant_logs` as `l4` ON `custome_module`.`id` = `l4`.`nmodule_id` AND `l4`.`ntype` = 5
WHERE `custome_module`.`nbranch_id` = '142'
GROUP BY `custome_module`.`id`
and also in Codeigniter
$this->db->select('custome_module.id, custome_module.name, min(l1.nmark_completed) as call_waiter, min(l2.nmark_completed) as bill,min(l3.nmark_completed) as tray,min(l4.nmark_completed) as ordera');
$this->db->from("custome_module");
$this->db->join('restaurant_logs as l1', 'custome_module.id = l1.nmodule_id AND l1.ntype = 1', 'left');
$this->db->join('restaurant_logs as l2', 'custome_module.id = l2.nmodule_id AND l2.ntype = 2', 'left');
$this->db->join('restaurant_logs as l3', 'custome_module.id = l3.nmodule_id AND l3.ntype = 6', 'left');
$this->db->join('restaurant_logs as l4', 'custome_module.id = l4.nmodule_id AND l4.ntype = 5', 'left');
$this->db->where('custome_module.nbranch_id', $this->data['user_session']['nid']);
$this->db->get();