codeigniter where() postgresql - php

What is the format in codeigniter
SELECT seq,
code,
discipline,
bacc
FROM "psced-disc" p
JOIN table2 t
ON p.code=t.psced_id
WHERE t.tableID=table2;
I already tried this and its working but without the WHERE clause
$this->db->select('seq, code, discipline,bacc');
$this->db->from('psced-disc p');
$this->db->join('table2 t', 'p.code=t.psced_id');
$query = $this->db->get();
return $query->result();
I want to add the WHERE t.tableID=table2;
Can anyone help me with this. I'm getting stuck at it.

try this code:
$this->db->select('seq, code, discipline,bacc');
$this->db->from('psced-disc p');
$this->db->join('table2 t', 'p.code=t.psced_id');
$this->db->where(array("t.tableID"=>"table2")); // or $this->db->where("t.tableID = 'table2'");
$query = $this->db->get();
return $query->result();

Related

Inner join query in codeigniter

code:
public function draft_post($idd)
{
$this->db->select('*');
$this->db->from('registration');
$this->db->join('draft_registration', 'registration.user_id= draft_registration.user_id','INNER');
$this->db->where('registration.user_id', $idd);
$query = $this->db->get();
$result = $query->result_array();
return $result;
}
In this codes, I have two table i.e. registration and draft_registration. Now, What am I doing here I want to run inner join in Codeigniter. Now, What happening when I hit this query on phpmyadmin it shows wrong data i.e. I have two rows in draft_registration and one row in registration table but it always shows two table which is wrong and my query looks like when I was print as mention below:
SELECT *
FROM `registration`
INNER JOIN `draft_registration` ON `registration`.`user_id`= `draft_registration`.`user_id`
WHERE `registration`.`user_id` = '20181121064044'
So, How can I resolve this issue? Please help me.
Thank You
$this->db->select('*'); //This code get all rows from both table.If you want a particular row you mention the column name.
For example:
$this->db->select('registration.name,draft_registration.city,draft_registration.state');
Specify column that you want to select. Or if you want select all column of your table, you can use :
SELECT registration.* with backticks `` on column name
Use the Below Code
public function draft_post($idd)
{
$this->db->select('registration.*,draft_registration.*');
$this->db->from('registration');
$this->db->join('draft_registration', 'registration.user_id= draft_registration.user_id');
$this->db->where('registration.user_id', $idd);
$query = $this->db->get();
$result = $query->result_array();
return $result;
}
Or you can use with objects
public function draft_post($idd)
{
$this->db->select('a.*,b.*');
$this->db->from('registration a');
$this->db->join('draft_registration b', 'a.user_id= b.user_id');
$this->db->where('a.user_id', $idd);
$query = $this->db->get();
$result = $query->result_array();
return $result;
}

Multiple Join query in codeigniter?

function getReceiptData($receipt_id){
$this->db->select('class.name as cname,student.name as sname,student.father_name as fname,
student.student_parent_email rmail,student.address as raddress,admin.name as aname,fee_particular_name as pname,
fee_particular_discount as discount,fee_particular_amount as pamount,fee_category_id as cat_id, fee_collections.*');
$this->db->from('fee_collections');
$this->db->join('class','class_id = fee_collection_class_id');
$this->db->join('student','student_id = fee_collection_roll_id');
$this->db->join('fee_particulars','fee_particular_id = fee_collection_particular_id');
$this->db->join('admin','admin_id = fee_collection_added_by');
$this->db->where('fee_collection_id',$receipt_id);
$query = $this->db->get();
return $query->row();
}
this would generate NULL output why?
When you are using join query, Please use alias like given below.
$this->db->from('fee_collections AS ac');
$this->db->join('class AS cs','cs.class_id = ac.fee_collection_class_id');
Please try this, This will help you to get output.
Thanks for your edit!
This edit will be visible only to you until it is peer reviewed.
function getReceiptData($receipt_id){
$this->db->select('class.name as cname,student.name as sname,student.father_name as fname,
student.student_parent_email rmail,student.address as raddress,admin.name as aname,fee_particular_name as pname,
fee_particular_discount as discount,fee_particular_amount as pamount,fee_category_id as cat_id, fee_collections.*');
$this->db->from('fee_collections');
$this->db->join('class','class_id = fee_collection_class_id', 'left');
$this->db->join('student','student_id = fee_collection_roll_id', 'left');
$this->db->join('fee_particulars','fee_particular_id = fee_collection_particular_id', 'left');
$this->db->join('admin','admin_id = fee_collection_added_by');
$this->db->where('fee_collection_id',$receipt_id);
$query = $this->db->get();
return $query->row();
}
#Ganesh : Please check my below mentioned query, As per db schema shared by you it's in my local system.
$this->db->select('c.name as cname, s.name as sname,fc.*');
$this->db->from('fee_collections fc');
$this->db->join('class c', 'c.class_id = fc.fee_collection_class_id', 'left');
$this->db->join('student s', 's.student_id = fc.fee_collection_roll_id', 'left');
$this->db->join('fee_particulars fp', 'fp.fee_particular_id = fc.fee_particular_id', 'left');
$this->db->join('admin a', 'a.admin_id = fc.fee_collection_added_by');
$this->db->where('fc.fee_collection_receipt', $receipt_id);
$query = $this->db->get();
return $query->row();
Please change select() line as per your table field name.
Let me know if you have any error.
function getReceiptData($receipt_id){
$this->db->select('class.name as cname,student.name as sname,student.father_name as fname,
student.student_parent_email rmail,student.address as raddress,admin.name as aname,fee_particular_name as pname,
fee_particular_discount as discount,fee_particular_amount as pamount,fee_category_id as cat_id, fee_collections.*');
$this->db->from('fee_collections');
$this->db->where('fee_collection_id',$receipt_id);
$this->db->join('class','class_id = fee_collection_class_id');
$this->db->join('student','student_id = fee_collection_roll_id');
$this->db->join('fee_particulars','fee_particular_id = fee_collection_particular_id');
$this->db->join('admin','admin_id = fee_collection_added_by');
$query = $this->db->get();
return $query->row();
}
You are not getting any sql error means your query is right but you are not getting data means your condition is wrong.
Without see the table structure its tough to say exact reason. By seeing your code look like right but you can check same sql query by writing raw sql query. It seems some table not meet joinned condition.
function getReceiptData($receipt_id){
$this->db->select('class.name as cname,student.name as sname,student.father_name as fname,
student.student_parent_email rmail,student.address as raddress,admin.name as aname,fee_particular_name as pname,
fee_particular_discount as discount,fee_particular_amount as pamount,fee_category_id as cat_id, fee_collections.*');
$this->db->from('fee_collections');
$this->db->join('class','class_id = fee_collection_class_id', 'left');
$this->db->join('student','student_id = fee_collection_roll_id', 'left');
$this->db->join('fee_particulars','fee_particular_id = fee_collection_particular_id', 'left');
$this->db->join('admin','admin_id = fee_collection_added_by');
$this->db->where('fee_collection_id',$receipt_id);
$query = $this->db->get();
return $query->row();
}

how to write simple query into codeigniter query using join right

how to write simple query into codeigniter query using join righ?????
$query = $this->db->query("Select staff_permissions_list.perm_type,staff_permissions_list.permission_key,staff_permissions_list.permission_label,
staff_permissions_list.id, staff_role_permissions.permission_id as p_id,staff_role_permissions.role_id
FROM staff_role_permissions
RIGHT JOIN staff_permissions_list ON staff_role_permissions.permission_id=staff_permissions_list.id
AND staff_role_permissions.role_id=$id WHERE staff_permissions_list.perm_type=0
ORDER BY staff_permissions_list.id ASC
");
if ($query->num_rows() > 0) {
return $query->result_array();
}
$this->db->select('book_id, book_name, author_name, category_name');
$this->db->from('books');
$this->db->join('category', 'category.category_id = books.category_id', 'right');
$query = $this->db->get();
you can get data using this method of right join
How about that ?
$query = $this->db
->select("Select staff_permissions_list.perm_type,staff_permissions_list.permission_key,staff_permissions_list.permission_label,staff_permissions_list.id, staff_role_permissions.permission_id as p_id,staff_role_permissions.role_id")
->from("staff_role_permissions AS srp")
->join("staff_permissions_list AS spl","srp.permission_id = spl.id","right")
->where("spl.perm_type","0")
->where("srp.role_id",$id)
->order_by("spl.id","ASC")
->get();
i put the role_id to the where section - maybe you need to put it back (not sure what you want to achieve here)

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 write an SQL query in query CI syntax?

I have below query, which I need to write in Model of Codeignitor
SELECT DISTINCT make FROM equipment_nonconnected_master WHERE Equipment_NonConnected_Type_Master_ID IN (select Equipment_NonConnected_Type_Master_ID FROM equipment_nonconnected_type_master)
I am only aware of the select query simply written in Codeignitor as given below:
EXAMPLE:
$this->db->distinct();
$this->db->select('make');
$this->db->order_by('make', 'asc');
$query = $this->db->get('carriermodels');
Can someone help me writing the Query in CI syntax
You can do so by using simple where() function
$subquery="SELECT
Equipment_NonConnected_Type_Master_ID
FROM
equipment_nonconnected_type_master";
$this->db->distinct();
$this->db->select('make');
$this->db->from('carriermodels');
$this->db->where('Equipment_NonConnected_Type_Master_ID IN('.$subquery.')');
$this->db->order_by('make', 'asc');
$query = $this->db->get();
Or better to use join
$this->db->distinct();
$this->db->select('c.make');
$this->db->from('carriermodels c');
$this->db->join('equipment_nonconnected_type_master m','c.Equipment_NonConnected_Type_Master_ID =m.Equipment_NonConnected_Type_Master_ID ');
$this->db->order_by('c.make', 'asc');
$query = $this->db->get();

Categories