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.
Related
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;
}
I have a database table with separate date and time column. To get particular I execute query in mysql in following manner
select * from table_A where empID='A1201' order by TIMESTAMP(date,time) desc limit 1
How can I convert this particular for codeigniter? I tried in following manner but it's not working
$column = 'TIMESTAMP(date,time)';
$this->db->select('*');
$this->db->where('empID', 'A1201');
$this->db->from('table_A');
$this->db->order_by($column, 'desc');
$this->db->limit(1);
$query = $this->db->get();
$data = $query->result();
return $data;
This query result in error since it executes in following manner ORDER BY TIMESTAMP(date DESC, time) DESC while the correct way is ORDER BY TIMESTAMP(date,time) DESC. What will be the correct way for codeigniter using active record
try this
$query = $this->db
->select('*')
->where('empID', 'A1201')
->from('table_A')
->order_by($column, 'desc', false)
->limit(1)
->get();
order by comes with a 3rd option - just set this to false
You can get more infos about that in their documentation here
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)
I try this code
function catstore($id)
{
$this->db->distinct();
$this->db->select('store_id');
$this->db->from('products');
//$this->db->join('products','products.store_id = products.store_id');
$this->db->where('cat_id', $id);
$result = $this->db->get();
return $result->result();
}
it produces only the distinct store_id but i want all column of the table with distinct rows based on store_id
Suggest me any option
Thanks in advance
You can try using group_by
function catstore($id)
{
$this->db->select('*');
$this->db->from('products');
//$this->db->join('products','products.store_id = products.store_id');
$this->db->where('cat_id', $id);
$this->db->group_by('store_id');
$result = $this->db->get();
return $result->result();
}
I know this question is already long time ago, but one may be looking for the same problem like me, and this is how I solved it.
you can omit the select function altogether. If you are selecting all (*) from a table you do not need to use this function. When omitted, CodeIgniter assumes you wish to SELECT *
See http://codeigniter.com/user_guide/database/active_record.html#select
Or..
You can pass comma separated values to select function. So pass all column names separated by comma that you want to select
function catstore($id)
{
$this->db->distinct();
$this->db->select('store_id, column1, column2, cloumn3');
$this->db->from('products');
//$this->db->join('products','products.store_id = products.store_id');
$this->db->where('cat_id', $id);
$result = $this->db->get();
return $result->result();
}
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;
}