【问题标题】:Phalcon MySQL query with Bitwise (Shift-Left Operators)Phalcon MySQL 按位查询(左移运算符)
【发布时间】:2015-11-04 09:38:56
【问题描述】:

我有用户模型,我需要在其中选择一些其他相关模型,但是我的查询不是标准的,所以我认为我不能使用 find() 或 query:: 来获得结果,所以我在想使用“modelmanager”及其 phql 来完成这项工作,但是结果我得到了空集,为什么会这样

SQL 查询是

 SELECT title FROM var_religion WHERE (15 & (1 << (id - 1))) ORDER BY id DESC

但是,当使用以下功能 insde 用户模型时,它似乎没有任何想法? ```

 public function partner(){

//$p_religions = VarReligion::find(
//    array(
 //        "conditions" => " (:var: & (1 << (id - 1))) ",
 //        "bind"       => array("var"=> $this->religion),
 //        "order"     => "id DESC"
 //    )
 //);
 $phql = "SELECT title FROM var_religion WHERE (15 & (1 << (id - 1))) ORDER BY id DESC";
 $p_religions = $this->manager->executeQuery($phql);
 var_dump($p_religions);
}

我没有输出为什么?我该如何做这个查询?

注意: 如果 phalcon mysql 模型无法做到这一点,我宁愿至少需要一个 php equelent 来将此逻辑转换为

SELECT title FROM var_religion WHERE id IN (1,,,,,4);

【问题讨论】:

  • 你能在 mysql 中使用 shell 程序或 phpMyAdmin 或类似程序直接运行查询吗?那它成功了吗?
  • 当然查询完全有效并返回结果...
  • 如果你不知道什么是左撇子运算符,请阅读下面的stackoverflow.com/questions/6670611/bitwise-shift-in-mysql
  • 总是检查查询是否真的在拉取结果,即使 php 似乎没有得到它们。我知道左边的 shift 运算符,谢谢
  • 感谢如果 phalcon mysql 模型无法做到这一点,我宁愿至少需要一个 php equelent 将此登录名转换为 WHERE id IN (1,,,,,4);跨度>

标签: php mysql phalcon


【解决方案1】:

通过这次尝试管理了一个相似的请求:

$religions = new \Application\Models\Religions();

$sql = 'SELECT id FROM religions WHERE
        (15 & (1 << (id - 1))) > 0  ORDER BY id DESC';

$result = new \Phalcon\Mvc\Model\Resultset\Simple(
    null,
    $religions,
    $religions->getReadConnection()->query($sql)
);

print_r($result->toArray());

请注意,由于 PgSQL 类型限制,我必须执行 &gt; 0。其他尝试是将查询本身缩短为:

 SELECT id, (15 & (1 << (id - 1))) as rank FROM religions HAVING rank > 0

Phalcon docs 中的更多信息。

你使用的Phql不支持位运算符,所以你需要这样破解。


PS: 在上述查询中使用SELECT * 时,您可以从模型函数中受益,例如:

$result = new \Phalcon\Mvc\Model\Resultset\Simple(null, $keyword, $keyword->getReadConnection()->query($sql));

foreach($result as $r) {
    var_dump($r->getId());
}

【讨论】:

    猜你喜欢
    • 2016-02-26
    • 1970-01-01
    • 1970-01-01
    • 2014-08-02
    • 2019-12-04
    • 2013-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多