CodeIgniter Forums
Is it possible to do a `where id=(select id from table);` sql statement ? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Is it possible to do a `where id=(select id from table);` sql statement ? (/showthread.php?tid=80610)



Is it possible to do a `where id=(select id from table);` sql statement ? - ShinProg - 11-21-2021

Hello,
I'd like to achieve this sql using the QueryBuilder class. Is it possible ?
Code:
UPDATE Series
  SET index = index+1
where index >= (SELECT index FROM Series WHERE field_id = 5)

-- or --

UPDATE Series
  SET index = ( SELECT max(index) FROM Series )
  WHERE field_id = 3

I'm currently using two different database calls (one select, one update), but since I'm using a very small amount of data, optimization is not a priority.


RE: Is it possible to do a `where id=(select id from table);` sql statement ? - sammyskills - 11-22-2021

Well, let's say I'm being curious, but why don't you run the inner select queries first, store the value in a variable, then run the other select or update separately using the already existing value from the saved variable?


RE: Is it possible to do a `where id=(select id from table);` sql statement ? - includebeer - 11-22-2021

Your query is too complex for the Query Builder. I think you should use query binding instead: https://codeigniter.com/user_guide/database/queries.html#query-bindings