dotelauv682684 2017-03-11 08:23
浏览 107

如何在php codeigniter中一起使用`where`和`in`子句?

I have two tables:

1) Users
2) Trustmembers

I want to take all userId's from Trustmembers and compare with Id of Users table and then i want to print all the names from Users table of matching id's.

In sql I can write the query like

select name from users where Id IN(select userId from Trustmembers); 

How to write the same query in Model(MVC) of php codeigniter to get the same result?

Or any other way except using Where_in??

  • 写回答

1条回答 默认 最新

  • dounieyan2036 2017-03-11 08:27
    关注

    You can do like this,

    METHOD 1:

    $this->db->select('name')->from('users');
    $this->db->where('Id IN (SELECT userId FROM `Trustmembers`)', NULL, FALSE);
    $result = $this->db->get();
    

    NULL, false will be used to not escape the query as by default.

    METHOD 2

    $this->db->select('name')->from('users');
    $this->db->where_in('Id', 'SELECT userId FROM `Trustmembers`');
    $result = $this->db->get();
    

    METHOD 3 : subquery library

    $this->db->select('name')->from('users');
    $subquery = $this->subquery->start_subquery('where_in');
    $subquery->select('userId')->from('Trustmembers');
    $this->subquery->end_subquery('Id', FALSE);
    $result = $this->db->get();
    

    I hope this will help.

    评论

报告相同问题?

悬赏问题

  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64