Tuesday, February 8, 2022

[FIXED] SQL query using code igniter

Issue

How can I write this query to a code igniter query?

WHERE
  SmallVersion.ID =
  (
    SELECT ChildSmallVersionID AS ID FROM SmallVersion
    WHERE ID = $id
  )

this is how is looks so far:

         ->where('SmallVersion.ID', ("SELECT `ChildSmallVersionID` AS ID
                  FROM `SmallVersion` WHERE ID = $id"));

Solution

Mateusz Wojtula's answer looks ok but you can protect your query

$strSubQuery = $this->db
    ->select("ChildSmallVersionID AS ID")
    ->from("SmallVersion")
    ->where("ID",$id)
    ->get_compiled_select();

$where = "SmallVersion.ID = (".$strSubQuery.")";
$this->db->where($where, NULL, FALSE);


Answered By - sintakonte

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.