dongxun5349 2017-07-19 08:37
浏览 34
已采纳

从具有相同值的列和具有不同值的另一列的多个行中进行选择

I am using codeigniter, i want to select data from a table which has two columns lab_id and service_id

lab_id        service_id
  1              1
  1              2
  3              1

and i only want to select lab_id "1" which has both 1 and 2 as service id. currently i am using following code but not working.

<pre>    
    $this->db->select('lab_id');
    $this->db->from('labs_services_mapping');
    foreach ($data as $value) {
        $this->db->where('service_id',$value['id']);
    }
    $this->db->group_by('lab_id');
    $query = $this->db->get()->result();
</pre>

$data is an array with service_id

  • 写回答

1条回答 默认 最新

  • dongwei1234 2017-07-19 08:46
    关注

    this should do the trick

    $this->db->select('lab_id');
    $this->db->from('labs_services_mapping');
    foreach ($data as $value) {
        $this->db->or_where('service_id',$value['id']);
    }
    $this->db->group_by('lab_id');
    $query = $this->db->get()->result();
    

    you can try to use where_in too

    $this->db->select('lab_id');
    $this->db->from('labs_services_mapping');
    $this->db->where_in("service_id", $data);
    $this->db->group_by('lab_id');
    $query = $this->db->get()->result();
    

    Since you want a static result you should do this

    $this->db->select('lab_id');
    $this->db->from('labs_services_mapping');
    $this->db->where_in("service_id", [1,2]);
    $this->db->group_by('lab_id');
    $query = $this->db->get()->result();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?