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();
}
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;
}
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.
Hello i want to join three tables but it fetch all data from user table, i just want firstname and lastname parameter in that
Here is my join query. i want something like join(user.firstname).
$this->db->select('*');
$this->db->from('post');
$this->db->join('user', 'user.id = post.cop_id');
$this->db->join('source', 'post.source_id = source.id');
$query = $this->db->get();
return $query->result();
You can do something like this:
$this->db->select('post.*, source.*, user.firstname, user.lastname');
$this->db->from('post');
$this->db->join('user', 'user.id = post.cop_id');
$this->db->join('source', 'post.source_id = source.id');
$query = $this->db->get();
return $query->result();
The table.* indicates that you want all the fields from that table.
change $this->db->select('*');
to $this->db->select('user.firstname, user.lastname');
You can define that in the select:
$this->db->select(['post.id', 'post.title', 'post.description', 'user.firstname']);
Take a look at this answer https://stackoverflow.com/a/15402766/1897484
i have a table like this
name|subjects|location
......................
BUET|CSE|Dhaka
BUET|EEE|Dhaka
RUET|CE |Rajshahi
RU |CE |Rajshahi
here all the rows are distinct.And if I use
$this->db->select('*') and $this->db->distinct()
it would select all the rows of BUET but i only want like this
name|subjects|location
......................
BUET|CSE|Dhaka
RUET|CE |Rajshahi
RU |CE |Rajshahi
That means only the first column must be distinct and select all the columns as usual.And it would work if i use $this->db->select('name') and $this->db->distinct(). Then what would be about the other columns??
As my original table has many columns so I want to use $this->db->select('*'). I think $this->db->distinct() does not take any column as parameter. It differentiate result based on select. How can I do this?
Try like this
$this->db->select('DISTINCT `name`'); //You may use $this->db->distinct('name');
$this->db->select('*');
Select the distinct values by names.And your SELECT spelt wrong,may be its a typing mistake.And you can also use GROUP BY like
$this->db->select('*');
$this->db->group_by('name');
$this->db->select('*');
$this->db->group_by('column_name');
$this->db->from('tbl_name');
$query = $this->db->get();
return $query->result();
try this
You should use group_by in place of distinct.
because distinct will return unique rows. but to have unique column by row you should do group by.
Hope this helps.
$this->db->select('column names');
$this->db->distinct();
$query = $this->db->get('table name');
$query->result();
$this->db->select('job,id');
$this->db->group_by('job');
$query= $this->db->get('technicals')->result_array();
if(!empty($query)){
return $query;
}else{
return false;
}
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;
}