使用代码点火器的SQL查询

时间:2017-03-29 05:16:20

标签: codeigniter

如何将此查询写入代码点火器查询?

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

到目前为止看起来如何:

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

2 个答案:

答案 0 :(得分:0)

无法在Active Records中创建括号,但您可以直接在子句中编写它:

$where = "SmallVersion.ID =
  (
    SELECT ChildSmallVersionID AS ID FROM SmallVersion
    WHERE ID = $id
  )";
$this->db->where($where, NULL, FALSE);

如果第三个参数设置为FALSE,则CodeIgniter将无法保护您的查询。

活动记录的文档: https://www.codeigniter.com/user_guide/database/query_builder.html#looking-for-specific-data

答案 1 :(得分:0)

Mateusz Wojtula的回答看起来不错,但你可以保护你的查询

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