0

My sql nested query is something like this:-

$this->db->where_in('order.order_id', "SELECT `booking_id` FROM `booking` where `booking_id`=$id");

The problem is, the second parameter in where_in is considered as a string not as a query; since it comes under quotes, while printing last executed query through the function:

print_r($this->db->last_query());

so it returns nothing from the database.

How can I tolerate this problem can anybody please help ?

Learning
  • 198
  • 1
  • 11
VishnuPrasad
  • 1,078
  • 5
  • 17
  • 36
  • Check this http://stackoverflow.com/questions/6047149/subquery-in-codeigniter-active-record – Sadikhasan Mar 27 '14 at 09:00
  • already gone through that, but not getting; cant able to pass variable as like in my query – VishnuPrasad Mar 27 '14 at 09:05
  • I'm unsure why you are using "where_in" when a subquery can only return one column, not a range of values. Also your subquery is completely redundant, you are selecting `booking_id` WHERE `booking_id` is provided by you in your $id variable, which will therefore just return the value of $id. What exactly are you trying to achieve with your query? – AJReading Mar 27 '14 at 09:16

3 Answers3

0

you should use $this->db->where();

then pass in the whole where clause as a string.

YouSer
  • 393
  • 3
  • 12
0

You can use:

$this->db->query("Your Query here");

or you can do something like this:

$whereClaus = "Your where clause";

$this->db->where($whereClause); 
Marijn
  • 10,367
  • 5
  • 59
  • 80
Mohan
  • 4,677
  • 7
  • 42
  • 65
0

did you try this

$this->db->where_in('order.order_id', "SELECT `booking_id` FROM `booking` where `booking_id`=$id",FALSE);  // If you set it to FALSE, CodeIgniter will not try to protect your field or table names with backticks.

for more info check this url http://ellislab.com/codeigniter/user-guide/database/active_record.html

Dexter
  • 1,804
  • 4
  • 24
  • 53