CodeIgniter 3.1查询生成器中的参数绑定

2jcobegt  于 2022-12-07  发布在  其他
关注(0)|答案(1)|浏览(104)

我有个疑问

$column = 1234;
$sql = "SELECT * FROM TABLE WHERE COLUMN = ?";
$result = $this->db->query($sql,[$column])->row();

它完全可以工作,但是当我有union子句时,我必须传递参数两次

$sql = "SELECT * FROM TABLE1 WHERE COLUMN = ?
       UNION
       SELECT * FROM TABLE2 WHERE COLUMN = ?";
$result = $this->db->query($sql,[$column,$column])->result();

我们是否有类似下面的代码来实现上面的结果,其中我只能传递一个值,因为它们都需要相同的值。

$sql = "SELECT * FROM TABLE1 WHERE COLUMN = :column
       UNION
       SELECT * FROM TABLE2 WHERE COLUMN = :column";
$result = $this->db->query($sql,['column' => $column])->result();
6uxekuva

6uxekuva1#

您可以用途:

$column = 1234;
$sql = "SELECT * FROM TABLE1 WHERE COLUMN = " . $column . "
       UNION
       SELECT * FROM TABLE2 WHERE COLUMN = " . $column . ";
$result = $sql->result();

相关问题