Phalcon Framework中的HQL UNION查询

时间:2014-09-23 03:34:25

标签: union phalcon

昨天,当我在Phalcon中使用UNION进行查询语句时遇到了一个问题,我收到了一个错误。我在互联网上发现了这个问题。但似乎UNION不受支持。有人知道吗?

$phql = "SELECT DISTINCT key, type FROM (
          (SELECT DISTINCT key, type FROM TableA INNER JOIN TableB    WHERE b.id =  $userId)
          UNION
          (SELECT DISTINCT key, type FROM TableA INNER JOIN TableC WHERE c.id IN $groupIds)
) ";

1 个答案:

答案 0 :(得分:2)

你可以检查什么:
 1.检查 $ groupIds 是否不是数组。它应该只是一个字符串格式"(1,2,3)"在 IN 之后使用它  2.为查询中的所有表格命名(如我的示例 p1,p2,p3
 3.将params传递给查询更安全,而不是真正的变量

示例以下代码效果正常):

$db = \Phalcon\DI::getDefault()->get('db');
$stmt = $db->prepare("
    SELECT DISTINCT p3.id, p3.name FROM (
        (SELECT DISTINCT p1.id, p1.name FROM project p1 where p1.id = :from)
        UNION
        (SELECT DISTINCT p2.id, p2.name FROM project p2 where p2.id IN (:to))
    ) p3
");
$stmt->execute(['from' => 3, 'to' => implode(',', array(8))]);
$created = $stmt->fetchAll(\PDO::FETCH_ASSOC);