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;
}
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;
}
using code igniter! I have two tables student and student_class with foreign key student_id, i want to pick data which exists on student table but not found on class_student
here is my sql
function student_class(){
$this->db->SELECT ('student.student_id, student.firstname, student.middlename, student.lastname');
$this->db->FROM('student');
$this->db->WHERE('student.status',0);
$this->db->JOIN('student_class', 'student_class.student_id=student_class.student_id', 'left');
$this->db->where_not_in('student_class.student_id');
$query =$this->db->get();
return $query->result_array();
}
it does not work out!!
can i get help..
Try like this..
First find all student ids that are matched with student_class table.Then use $this->db->where_not_in to get your required result.
function student_class(){
$this->db->select ('student.student_id');
$this->db->from ('student');
$this->db->join('student_class', 'student.student_id=student_class.student_id', 'left');
$this->db->where('student.status',0);
$data = $this->db->result_array();//array of matched id's
$this->db->select('student_id,firstname, middlename,lastname');
$this->db->from('student');
$this->db->where_not_in($data);
$query = $this->db->get();
return $query->result_array();
}
Hope it will works.
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
$q = $this->db->select('
books.title,
reserved_books.id,
reserved_books.isbn,
reserved_books.price,
reserved_books.item_sold,
reserved_books.date_sold,
reserved_books.total_amount
')
->from('reserved_books')
->join('books','reserved_books.isbn= books.isbn')
->limit($limit,$offset);
how can i use distinct here in my query?
reserved_books.isbn is the unique one.
Try Below:
$this->db->distinct();
but Distinct will not always work. You should add ->group_by("name_of_the_column_which_needs_to_be unique");
$this->db->group_by('column_name');
You don't need any join
$query=$this->db->distinct()->select('table_attribute')->where('condition')->get('table_name');
return $query->result();
Adds the "DISTINCT" keyword to a query before Select
$this->db->distinct();
Refrence
Here is a way to do this
$select = array(
'books.title',
'reserved_books.id',
'DISTINCT reserved_books.isbn',
'reserved_books.price',
'reserved_books.item_sold',
'reserved_books.date_sold',
'reserved_books.total_amount'
);
$q = $this->db
->select($select)
->from('reserved_books')
->join('books','reserved_books.isbn= books.isbn')
->group_by('reserved_books.isbn')
->limit($limit,$offset);
Here is best way to remove duplicate entry.
$query = $this->db->select("inf.id, inf.sku, inf.fab_id, inf.sku, inf.amount, inf.min_length, inf.num_of_pieces, inf.width ,inf.type")
->join("retailcat","retailcat.sku=SUBSTRING(inf.sku,1,19) AND retailcat.category=218","INNER")
->distinct()
->get("input_factor inf");
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();
}