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?
我一直试图显示没有重复的行,但是查询没有正常工作。我认为问题在于很多人之间的关系,因为一个“间歇期”有很多“citas”。因此,例如,我只想显示:“从8:00到15:00(这是一个间隔时间)”到日期(cita)“27/08/1988”。我应该做什么?
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)
当前列表(无序列表,但重复存在)
2 个解决方案
#1
2
You can use $this->db->distinct()
and add selecting primary key to remove duplicate:
您可以使用$this->db->distinct()并添加选择主键以删除重复:
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();
}
#2
1
Use the keyword DISTINCT
in your query
在查询中使用关键字DISTINCT
reference : https://dev.mysql.com/doc/refman/5.7/en/distinct-optimization.html
参考:https://dev.mysql.com/doc/refman/5.7/en/distinct-optimization.html
#1
2
You can use $this->db->distinct()
and add selecting primary key to remove duplicate:
您可以使用$this->db->distinct()并添加选择主键以删除重复:
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();
}
#2
1
Use the keyword DISTINCT
in your query
在查询中使用关键字DISTINCT
reference : https://dev.mysql.com/doc/refman/5.7/en/distinct-optimization.html
参考:https://dev.mysql.com/doc/refman/5.7/en/distinct-optimization.html