如何在zend2中编写复杂的sql查询

时间:2013-03-07 08:06:25

标签: mysql zend-framework2

我想在zend框架2中编写如下的mySQL查询。

如何在zend Db选择约定中转换它?

    SELECT profiles.*, 
    user.email,
    user.first_name,
    user.last_name,
    portfilio_images.profileImage,
    images.url AS imgurl
    FROM profiles

    INNER JOIN user ON user.user_id = profiles.ownerId

    LEFT JOIN (
        SELECT *
        FROM portfilio_images
        WHERE portfilio_images.profileImage = '1'
    ) as portfilio_images ON portfilio_images.profileId = profiles.id

    LEFT JOIN images ON images.id = portfilio_images.imageId

    WHERE profiles.ownerId != '4' AND (profiles.name LIKE '%a%' OR user.first_name LIKE '%a%' OR user.last_name LIKE '%a%')

    GROUP BY user.user_id

2 个答案:

答案 0 :(得分:1)

您还可以使用Zend \ Db \ Sql \ Select,它允许您以编程方式构建复杂查询。请参阅此处的文档:

http://zf2.readthedocs.org/en/release-2.1.1/modules/zend.db.sql.html#zend-db-sql-select

答案 1 :(得分:0)

您可以随时执行原始查询:

$sql = "SELECT * FROM test";

$statement = $abAdapter->query($sql);
$result = $statement->execute();

$resultSet = new ResultSet;
$resultSet->initialize($result);

return $resultSet;

或者如果你想添加一些参数

$sql = "SELECT * FROM test where col = ?";

$statement = $dbAdapter->query($sql);
$result = $statement->execute(array(99));

$resultSet = new ResultSet;
$resultSet->initialize($result);

return $resultSet;