How to write an SQL query in query CI syntax? - php

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();

Related

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)

Translating ORDER BY COUNT (*) DESC in codeigniter

function getCategory($year){
if(!$year){
$year=2017;
}
$q = $this->msdb->query("SELECT category_code, COUNT(*) AS numb
FROM easypm_sales_orders WHERE YEAR(sales_date) = $year
GROUP BY category_code ORDER BY COUNT(*) DESC");
return $q->result();
}
So, I have this model function and I want to translate the SQL query bit into codeigniter. I tried:
function getCategory($year){
if(!$year){
$year=2017;
}
$this->db->select('category_code');
$this->db->count("* as 'numb'");
$this->db->from('easypm_sales_orders');
$this->db->where('YEAR(sales_date)', $year);
$this->db->group_by('category_code');
$q = $this->db->order_by('numb', 'desc');
return $q->result();
}
but no luck, is there something I'm doing wrong?
try this
$query = $this->db
->select("category_code, count(*) AS numb",false)
->from ("easypm_sales_orders")
->where("YEAR(sales_date)",$year)
->group_by("category_code")
->order_by("numb","DESC")
->get();
return $query->result();
I believe it is necessary to use DB::raw() to represent a WHERE clause involving a function of a database column. Also, while it is possible to get a count from a Laravel query without using DB::raw(), if you want to assign a custom alias to that count it is also needed here. Consider the following code:
$this->db->select(DB::raw('count(*) as numb, category_code'))
$this->db->from('easypm_sales_orders');
$this->db->whereRaw('YEAR(sales_date) = ?', $year);
$this->db->group_by('category_code');
$q = $this->db->order_by('numb', 'desc');
return $q->result();
Codeigniter active record has no count method.
only count_all_results and count_all and they are used differently
You need replace your select options like this and remove $this->db->count("* as 'numb'");
$this->db->select('category_code');
$this->db->select('COUNT(*) numb');
Rest of your code OK.

How to make Join Query model in CodeIgniter

How to write join query in codeigniter... I want only model like this Select query-
public function getData($col, $table, $where = array())
{
$this->db->select($col);
$this->db->from($table);
$this->db->where($where);
$query = $this->db->get();
$result = $query->result();
return $result;
}
Plz help
$this->db->select('*');
$this->db->from('blogs');
$this->db->join('comments', 'comments.id = blogs.id');
http://ellislab.com/codeigniter/user-guide/database/active_record.html#select
Please go through the user guide before posting it on stackoverflow
join query is there..
try this
$this->db->join('second_table', 'second_table.id = first_table.id');
try like this
$this->db->select('*');
$this->db->from('first_table');
$this->db->join('second_table', 'second_table.col_name = first_table.col_name');
$query=$this->db->get();
if($query->num_rows()>0){
return $query->result_array();
}
for joining table you may use join() methods.
$this->db->from(table1)
$this->db->join('table2','table1.id=table2.table1_id','join options');
on condition means in which condition you want to join tables .
Join options is optional.
Join Options are: left, right, outer, inner, left outer, and right outer

codeigniter where() postgresql

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();

How to add an ORDER BY clause using CodeIgniter's Active Record methods?

I have a very small script to get all records from a database table, the code is below.
$query = $this->db->get($this->table_name);
return $query->result();
Using this syntax, how would I add a ORDER BY 'name' clause to my select query?
I get errors every time I stick the order by bit on the end.
I believe the get() function immediately runs the select query and does not accept ORDER BY conditions as parameters. I think you'll need to separately declare the conditions, then run the query. Give this a try:
$this->db->from($this->table_name);
$this->db->order_by("name", "asc");
$query = $this->db->get();
return $query->result();
CodeIgniter Documentation order_by()
Using this code to multiple order by in single query.
$this->db->from($this->table_name);
$this->db->order_by("column1 asc,column2 desc");
$query = $this->db->get();
return $query->result();
Simple and easy:
$this->db->order_by("name", "asc");
$query = $this->db->get($this->table_name);
return $query->result();
Just add the'order_by' clause to your code and modify it to look just like the one below.
$this->db->order_by('name', 'asc');
$result = $this->db->get($table);
There you go.
function getProductionGroupItems($itemId){
$this->db->select("*");
$this->db->where("id",$itemId);
$this->db->or_where("parent_item_id",$itemId);
/*********** order by *********** */
$this->db->order_by("id", "asc");
$q=$this->db->get("recipe_products");
if($q->num_rows()>0){
foreach($q->result() as $row){
$data[]=$row;
}
return $data;
}
return false;
}

Categories