Search code examples
codeigniter

SQL query using code igniter


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);