【问题标题】:Is there a way to create multiple 'WHERE' clause on a query?有没有办法在查询中创建多个“WHERE”子句?
【发布时间】:2020-07-05 15:39:00
【问题描述】:

假设我有这张表和它的行

我希望只得到post_idstudent_id 中的每一个

对于post_id = 1只需要得到这些

> 1. Capital of Australia
> 
> 2. Capital of Philippines
> 
> 3. What is the color of pineapple
> 
> 4. What is the color of mars

对于post_id = 7只需要得到这些

1. What is the law of war?
2. What is the color of tree?

这是我的查询 - 我需要选择学生 ID 以向每个学生显示他们的数据并发布每个学生数据的 ID。

public function results($id){
    $this->db->select('*');
    $this->db->from('results');
    $this->db->where('student_id', $id);
    $this->db->where('post_id', $id);
    $this->db->group_by('question');
    $query = $this->db->get();
    return $result = $query->result_array();
}

【问题讨论】:

  • $this->db->where(['student_id' => $id, 'post_id' => $id]);
  • student_id和post_id怎么能一样?
  • 在这里输入完全符合您期望的查询?如果不在这里添加一些示例数据的表模式。
  • 第一次查看时,我觉得student_answer 列保存了真实答案,反之亦然。您应该将问题表标准化为另一个具有真实答案的问题表,并在此处使用 id,因为它在此处复制了很多次。
  • 您似乎在问以下查询:“请向我展示给定 student_id 和给定 post_id 的所有问题”。这是一个逻辑上依赖于 2 个参数的查询。你的函数results只有一个参数。

标签: php mysql codeigniter codeigniter-3


【解决方案1】:

您可以按照Codeigniter 用户指南将包含所有conditional parameters 的关联数组传递给您的
$this->db->where($array); 子句。 check this

希望对你有帮助!

【讨论】:

    【解决方案2】:

    试试这个:

    $this->db->group_start()->where($first_condition)->group_end();
    $this->db->group_start()->where($second_condition)->group_end();
    $this->db->get()
    

    您可以在group_start()group_end() 之间修改每个条件。 希望它对你有用。

    【讨论】:

      猜你喜欢
      • 2010-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-13
      • 2012-02-17
      • 1970-01-01
      • 1970-01-01
      • 2016-02-27
      相关资源
      最近更新 更多