I have an SQL Query like this
return $this->db->query("SELECT * FROM `rincian_permintaan`
JOIN `permintaan` ON `permintaan`.`id_permintaan` = `rincian_permintaan`.`id_permintaan`
JOIN `users` ON `permintaan`.`id_peminta` = `users`.`user_id`
JOIN `cabang` ON `cabang`.`id_cabang` = `users`.`id_cabang`
JOIN `barang` ON `barang`.`id_barang` = `rincian_permintaan`.`id_barang`
JOIN `po` ON `rincian_permintaan`.`id_po` = `po`.`id_po` WHERE `po`.`id_cabang` = '201' AND `users`.`id_cabang` != 201 AND
NOT EXISTS(SELECT * FROM airwaybill WHERE airwaybill.id_rincian_permintaan = rincian_permintaan.id_rincian_permintaan)
ORDER BY rincian_permintaan.created_at DESC")->result_array();
I want to change the format to:
$this->db->select('*');
$this->db->from('tableName');
$this->db->join('...');
$this->db->where('...');
And this is what i've tried:
$this->db->select('*');
$this->db->from('rincian_permintaan');
$this->db->join('permintaan', 'permintaan.id_permintaan = rincian_permintaan.id_permintaan');
$this->db->join('users', 'permintaan.id_peminta = users.user_id');
$this->db->join('cabang', 'cabang.id_cabang = users.id_cabang');
$this->db->join('barang', 'barang.id_barang = rincian_permintaan.id_barang');
$this->db->join('po', 'rincian_permintaan.id_po = po.id_po');
$this->db->where('po.id_cabang', '201');
$this->db->where('users.id_cabang != 201');
$this->db->select('*');
$this->db->from('airwaybill');
$this->db->where('NOT EXISTS airwaybill.id_rincian_permintaan = rincian_permintaan.id_rincian_permintaan', '', FALSE);
return $this->db->get('rincian_permintaan')->result_array();
Error:
All you need is to generate sql string for subquery without executing it and use it in the outer query and produce the result.
$this->db->select('*');
$this->db->from('airwaybill');
$this->db->where('airwaybill.id_rincian_permintaan = rincian_permintaan.id_rincian_permintaan');
$sub_query = $this->db->get_compiled_select();
$this->db->select('*');
$this->db->from('rincian_permintaan');
$this->db->join('permintaan', 'permintaan.id_permintaan = rincian_permintaan.id_permintaan');
$this->db->join('users', 'permintaan.id_peminta = users.user_id');
$this->db->join('cabang', 'cabang.id_cabang = users.id_cabang');
$this->db->join('barang', 'barang.id_barang = rincian_permintaan.id_barang');
$this->db->join('po', 'rincian_permintaan.id_po = po.id_po');
$this->db->where('po.id_cabang', '201');
$this->db->where('users.id_cabang != 201 ');
$this->db->where('NOT EXISTS('.$sub_query.')');
$query = $this->db->get();
$result = $query->result_array();
return $result;
Related
here is my code of model
$this->db->select('*');
$this->db->from('vf_training_district', 'vf_training_firm', 'complain_form');
$this->db->where('complain_form.InstituteId', 'vf_training_firm.FirmId');
$this->db->where('complain_form.DistrictId', 'vf_training_district.DistrictId');
$query = $this->db->get();
return $result = $query->result_array();
getting error of unknown column complain_form.InstituteId. each and every column is same as in db
Hope this helps you :
$this->db->select(*);
$this->db->from('complain_form cf');
$this->db->join('vf_training_firm vftf', 'vftf.FirmId = cf.InstituteId');
$this->db->join('vf_training_district vftd', 'vftd.DistrictId = cf.DistrictId');
$query = $this->db->get();
return $result = $query->result_array();
for more : https://www.codeigniter.com/user_guide/database/query_builder.html#selecting-data
You need JOIN:
SELECT *
FROM complain_form
JOIN vf_training_firm ON complain_form.InstituteId = vf_training_firm.FirmId
JOIN vf_training_district ON complain_form.InstituteId = vf_training_district.DistrictId
using Query Builder class Codeigniter:
$row = $this->db->select(*)
->from('complain_form')
->join('vf_training_firm', 'complain_form.InstituteId = vf_training_firm.FirmId')
->join('vf_training_district', complain_form.InstituteId = vf_training_district.DistrictId)
->get();
return $row->result_array();
I am trying to combine 3-4 functions inside one function and call this inside another function. for example
public function todayordercount(){
$sql = "SELECT *,count(id) FROM `tbl_order` WHERE DATE(`date_added`) = CURDATE() and status='Delivered'";
$query = $this->db->query($sql);
if($query->num_rows()>0){
return $query->result_array();
}else{
return $query->row_array();
}
}
public function currentmonthcount(){
$sql = "SELECT sum(amount) as amt,sum(shipping) as shiping,sum(commission) as commission,sum(base_price) as base_price,sum(tax_amt) as tax,SUM(LENGTH(product_id) - LENGTH(REPLACE(product_id, ',', '')) + 1) as unit , count(id) FROM `tbl_order` WHERE MONTH(CURDATE())=MONTH(date_added) and status='Delivered'";
$query = $this->db->query($sql);
if($query->num_rows()>0){
return $query->result_array();
}else{
return $query->row_array();
}
}
public function mergeordercount(){
$query1 = $this->db->query("SELECT *,count(id) as today_order FROM `tbl_order` WHERE DATE(`date_added`) = CURDATE() and status='Delivered'");
$query2 = $this->db->query("SELECT sum(amount) as amt,sum(shipping) as shiping,sum(commission) as commission,sum(base_price) as base_price,sum(tax_amt) as tax,SUM(LENGTH(product_id) - LENGTH(REPLACE(product_id, ',', '')) + 1) as unit , count(id) FROM `tbl_order` WHERE MONTH(CURDATE())=MONTH(date_added) and status='Delivered'");
$query3 = $this->db->query("SELECT count(status) as status FROM `tbl_order` WHERE MONTH(CURDATE())=MONTH(date_added) and status='Return'");
$query4 = $this->db->query("SELECT count(response) as calls FROM `tbl_order` WHERE response='pending' or response='hold'");
$query5 = $this->db->query("SELECT count(id) as product FROM `tbl_product`");
$result1 = $query1->row_array();
$result2 = $query2->row_array();
$result3 = $query3->row_array();
$result4 = $query4->row_array();
$result5 = $query5->row_array();
return array_merge($result1, $result2, $result3, $result4, $result5);
}
you should do look like above code it will help you bettor.
Thank You...
I want to write a query in such a way that,if received id matches with $id then request should join with profile table as
r.sent_id = profile_id
If sent_id matches with $id,then
r.sent_id = profile_id
My model,
public function getRequestedDetails($id) {
$this->db->select('*');
$this->db->from('profile');
$stat = $this->db->where('recieved_id', $id);
if($stat)
$this->db->join('requests enter code hereas r', 'r.sent_id = profile_id', 'left');
$stat = $this->db->where('sent_id', $id);
if($stat)
$this->db->join('requests as r', 'r.recieved_id = profile_id', 'left');
$this->db->where('r.status', 1);
$query = $this->db->get();
$list = $query->result();
return $list;
}
I tried this,it shows the following error
Error Number: 1066
Not unique table/alias: 'r'
SELECT * FROM `profile` LEFT JOIN `requests` as `r` ON `r`.`sent_id` = `profile_id` LEFT JOIN `requests` as `r` ON `r`.`recieved_id` = `profile_id` WHERE `recieved_id` = '5' AND `sent_id` = '5' AND `sent_id` = '5' AND `r`.`status` = 1
Can anyone help me.Thanks.
Try this..
public function getRequestedDetails($id) {
$query = $this->db->select('*')
->from('profile')
->join('requests','(requests.sent_id = ' . $id . ' AND requests.sent_id = profile.profile_id)
OR (requests.recieved_id = ' . $id . ' AND requests.recieved_id = profile.profile_id'), 'left')
->where('profile.status',1)
->get();
$list = $query->result();
return $list;
}
Try this
SELECT *
FROM profile
LEFT JOIN requests AS requests
ON (requests.sent_id = profile.profile_id OR requests.recieved_id = profile.profile_id)
WHERE profile.recieved_id = '5'
AND profile.sent_id = '5'
AND profile.sent_id = '5'
AND requests.status = 1
below query work fine :
$query = $this->db->query("
select users.id as user_id ,users.username,traction_details.title,
traction_details.tr_id,tractions.buy_id,tractions.coupon_code,
tractions.coupon_property_id,tractions.expired
from
users,tractions,traction_details,coupon_cats
WHERE
tractions.id = traction_details.tr_id
AND
users.id = tractions.user_id
AND
coupon_cats.coupon_id = tractions.coupon_parent
AND
coupon_cats.cat_id = 13
AND
tractions.succ = 1
GROUP BY tractions.buy_id
ORDER BY traction_details.tr_id DESC
limit
$start,$end
");
$r = $query->result();
return $r ;
but below query doesn't work fine :
$this->db->select('users.id as user_id');
$this->db->select('users.username');
$this->db->select('traction_details.title');
$this->db->select('traction_details.tr_id');
$this->db->select('tractions.buy_id');
$this->db->select('tractions.coupon_code');
$this->db->select('tractions.coupon_property_id');
$this->db->select('tractions.expired');
$this->db->from('users,tractions,traction_details,coupon_cats');
$this->db->where('tractions.id ' , 'traction_details.tr_id');
$this->db->where('users.id ' , 'tractions.user_id');
$this->db->where('coupon_cats.coupon_id ' , 'tractions.coupon_parent');
$this->db->where('coupon_cats.cat_id =' , '13');
$this->db->where('tractions.succ =' , '1');
$this->db->group_by('tractions.buy_id');
$this->db->order_by('traction_details.tr_id', 'desc');
$this->db->limit($start, $end);
$result = $this->db->get()->result();
return $result;
where is my problem ?
Your query is wrong you don't need to repeat everything .. try
$q =
$this->db->select('users.id as user_id ,users.username,traction_details.title,
traction_details.tr_id,tractions.buy_id,tractions.coupon_code,
tractions.coupon_property_id,tractions.expired')
->from('users,tractions,traction_details,coupon_cats')
->where('tractions.id = traction_details.tr_id
AND
users.id = tractions.user_id
AND
coupon_cats.coupon_id = tractions.coupon_parent
AND
coupon_cats.cat_id = 13
AND
tractions.succ = 1')
->group_by('tractions.buy_id')
->order_by('traction_details.tr_id', 'desc')
->limit($start, $end)
->get();
$result = $q->result();
return $result;
Solved. I changed where clause to join clause :
$this->db->select('users.id as user_id');
$this->db->select('users.username');
$this->db->select('traction_details.title');
$this->db->select('traction_details.tr_id');
$this->db->select('tractions.buy_id');
$this->db->select('tractions.coupon_code');
$this->db->select('tractions.coupon_property_id');
$this->db->select('tractions.expired');
$this->db->from('tractions');
$this->db->join('traction_details' , 'traction_details.tr_id = tractions.id');
$this->db->join('users' , 'tractions.user_id = users.id');
$this->db->join('coupon_cats' , 'tractions.coupon_parent = coupon_cats.coupon_id');
$this->db->where('coupon_cats.cat_id =' , '13');
$this->db->where('tractions.succ =' , '1');
$this->db->group_by('tractions.buy_id');
$this->db->order_by('traction_details.tr_id', 'desc');
$this->db->limit($start, $end);
$result = $this->db->get()->result();
return $result;
I am trying to convert this query:
SELECT * FROM table WHERE condition1 = 'example' AND (date1 = 'date' OR renewal1 = 'renewal');
into CodeIgniter's Active Record format. I tried the following:
$q = $this->db->select('*');
$q = $this->db->from('table');
$q = $this->db->where('condition1 = condition');
$q = $this->db->where('date1 = date OR renewal = renewal');
$q = $this->db->get();
$results = $q->result();
But this did not have the desired affect. It seemed to not place the parenthesis around the second set of conditions, which caused the query to not work as expected. How else can I do this to represent what I want?
Thanks for the help!
You can use
$this->db->select('*');
$this->db->from('table');
$this->db->where('condition1 =',' condition');
$where = '(date1 = "date" OR renewal1 = "renewal")';// you can use your condition like this
$this->db->where($where);
$q = $this->db->get();
$results = $q->result();