i am trying to fetch records that have unique email address ( there are repeating emails as well in the database)
below is my query but it prints all the records
$this->db->distinct('email');
$this->db->select('*');
$this->db->from('booking');
$query = $this->db->get();
$data['booking']=$query->result();
Please help me to fix the issue . Thanks
You can achieve your results in two ways: distinct and group_by
For reference, please view here
$this->db->select('*');
$this->db->from('booking');
$this->db->distinct('email');
$query = $this->db->get();
return $query->result();
You can use group_by as alternatives way in your active record query.
$this->db->select('*');
$this->db->group_by('email');// add group_by
$query = $this->db->get('booking');
return $query->result();
do it like this...
$this->db->select('*');
$this->db->from('booking');
$this->db->group_by('email');
$query = $this->db->get();
$data['booking']=$query->result();
it will echo all unique email addresses except those duplicated email.
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 been trying to show rows without duplicates but the query isn't working properly. I think the problem is one to many relationship, because one 'intervaloHorario' has many 'citas'. So, for example, i want to show only: 'From 8:00 to 15:00 (this is an intervaloHorario)' to date (cita) '27/08/1988'. What should i do?
Controller
$this->Fechacita_Model->delete_duplicaterow();
Model
public function delete_duplicaterow() {
$this->db->select('
c.intervaloHorario','ci.cita'
);
$this->db->from('intervaloshorarios c');
$this->db->join('citas ci', 'ci.idCitas = c.idIntervaloHorario','left');
$this->db->group_by('c.idIntervaloHorario','ci.cita');
$query = $this->db->get();
return $query->num_rows();
}
Model(EDIT)
$this->db->select(array('c.intervaloHorario', 'ci.cita'));
$this->db->distinct();
$this->db->from('intervaloshorarios c');
$this->db->join('citas ci', 'ci.idCitas = c.idIntervaloHorario', 'left');
$this->db->group_by('c.idIntervaloHorario', 'ci.cita');
$query = $this->db->get();
$this->db->last_query();
return $query->num_rows();
Database
Current database
Screenshot
Current list (unordered list but duplicates persists)
You can use $this->db->distinct() and add selecting primary key to remove duplicate:
public function delete_duplicaterow() {
$this->db->select(array('c.intervaloHorario', 'ci.cita'));
$this->db->distinct();
$this->db->from('intervaloshorarios c');
$this->db->join('citas ci', 'ci.idCitas = c.idIntervaloHorario','left');
$this->db->group_by('c.idIntervaloHorario','ci.cita');
$query = $this->db->get();
return $query->num_rows();
}
Use the keyword DISTINCT in your query
reference : https://dev.mysql.com/doc/refman/5.7/en/distinct-optimization.html
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'am using CodeIgniter active records and I have a lack in knowledge of Joining multiple table columns in MySQL.
What I basically want to do, is to output all rows from a ci_categories table, but instead of showing ci_categories.component_id as a numeric, i wish to output the ci_components.name which have equal (or same) ci_components.id from another table ci_components by using the Active Records in CodeIgniter.
What I have already done, but with errors is:
Model:
public function getItemName($id){
$this->db->select('*');
$this->db->where('id', $id);
$result = $query->result();
return $result;
}
and View:
<?php echo $this->component_model->getItemName($cat['category']->com_id);?>
Any tips pls ?
$this->db->select('name')
->from('ci_components')
->where('ci_categories.component_id = ci_componenents.id');
$query = $this->db->get();
or
$this->db->select('name');
$this->db->from('ci_components');
$this->db->join('ci_categories', 'categories.component_id = ci_components.id');
$query = $this->db->get();
https://www.codeigniter.com/userguide2/database/active_record.html#select
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;
}