0

I want to write the given query in codeigniter , how can I do so?

SELECT * FROM `user` WHERE `id` IN (select `provider_id` from `services`);

I gone through codeigniter's where_in() but i'm getting wrong result , here what I have done,

$this->db->select('*');
$this->db->from(USER);
$this->db->where_in('id', 'select `provider_id` from `services`');
$query = $this->db->get();
return $query->num_rows();

it is returning 0, while the query above, when run directly in phpmyadmin, returns 1 record.

PhpCoder
  • 427
  • 5
  • 19

3 Answers3

1

You can use this simple way $this->db->query('your query');

Please check this post, hope it will help you Using Mysql WHERE IN clause in codeigniter

Community
  • 1
  • 1
Suvash sarker
  • 3,022
  • 1
  • 16
  • 19
1

You can pass direct queries in where class by passing second parameter as NULL and third parameter as FALSE

<?php 
$where = 'id IN (select `provider_id` from `services`)';
$this->db->select('*');
$this->db->from(USER);
$this->db->where($where, NULL, FALSE);
$query = $this->db->get();
return $query->num_rows();
Sundar
  • 4,318
  • 6
  • 32
  • 56
  • Hello thanks for helping, I tried all the 3(Suvash, Sundar, and Vinie) answers and all of them are working. Thanks for the alternate ways... – PhpCoder Aug 12 '15 at 10:13
1

Use $this->db->query();

$query=$this->db->query("SELECT * FROM `user` WHERE `id` IN (select `provider_id` from `services`)");
$num=$query->num_rows();
$result= $query->result();
Vinie
  • 2,910
  • 1
  • 14
  • 29