0

我有这个mysql查询:

SELECT 
    freeAnswers.*, 
    (SELECT `districtCode` 
         FROM `geodatas` 
         WHERE `zipCode` = clients.zipCode 
         GROUP BY `zipCode` 
         LIMIT 0, 1) as districtCode, 
    clients.zipCode, 
    clients.gender, 
    clients.startAge, 
    clients.endAge, 
    clients.mail, 
    clients.facebook, 
    surveys.customerId, 
    surveys.activityId, 
    surveys.name as surveyName, 
    customers.companyName, 
    activities.name as activityName 
    FROM freeAnswers, 
         clients, 
         surveys, 
         customers, 
         activities 
    WHERE freeAnswers.surveyId = surveys.id 
         AND surveys.customerId = customers.id 
         AND activities.id = surveys.activityId 
         AND clients.id = freeAnswers.clientId 
         AND customers.id = 1 
    ORDER BY activityName asc 
    LIMIT 0, 10

该查询在我的 mysql 服务器上是正确的,但是当我尝试在 Zend Framework 1.11 模型中使用它时,我收到此错误:Mysqli prepare error: Operand should contain 1 column(s)

拜托,谁能帮我让它运行良好?

最好的问候, 埃莱顿

4

1 回答 1

0

这是一些应该可以工作的代码。 Zend_Db_Select并没有真正提供一种在FROM不使用 a 的情况下从子句中的多个表中进行选择的方法,JOIN因此对于查询的一小部分,这对我来说有点骇人听闻。您最好的选择可能是JOIN在适当的情况下使用 s 重写查询。

    $subselect = $db->select()
                    ->from('geodatas', 'districtCode')
                    ->where('zipCode = clients.zipCode')
                    ->group('zipCode')
                    ->limit(1, 0);

    $from   = $db->quoteIdentifier('freeAnswers') . ', ' .
              $db->quoteIdentifier('clients')     . ', ' .
              $db->quoteIdentifier('surveys')     . ', ' .
              $db->quoteIdentifier('customers')   . ', ' .
              $db->quoteIdentifier('activities');

    $select = $db->select()
                 ->from(array('activities' => new Zend_Db_Expr($from)),
                        array('freeanswers.*',
                              'districtCode' =>
                                  new Zend_Db_Expr('(' . $subselect . ')'),
                              'clients.zipCode', 'clients.gender', 'clients.startAge',
                              'clients.endAge', 'clients.mail', 'clients.facebook',
                              'clients.customerId', 'clients.activityId',
                              'surveyName' => 'surveys.name', 'customers.companyName',
                              'activityName' => 'activities.name'))
                 ->where('freeAnswers.surveyId = surveys.id')
                 ->where('surveys.customerId = customers.id')
                 ->where('activities.id = surveys.activityId')
                 ->where('clients.id = freeAnswers.clientId')
                 ->where('customers.id = ?', 1)
                 ->order('activityName ASC')
                 ->limit(10, 0);

我说它是hackish的唯一原因是因为这条线:

->from(array('activities' => new Zend_Db_Expr($from)),

由于from()实际上只适用于一个表,因此我创建了一个Zend_Db_Expr并将相关性指定为表达式中的最后一个表名。如果您不传递 a Zend_Db_Expr,它将错误地引用逗号分隔的表名,或者如果您传递一个表名数组,它只使用第一个。当您传递Zend_Db_Expr没有名称的 a 时,它默认使用AS t在您的情况下也不起作用。这就是我按原样放置的原因。

除了提到的最后一件事之外,这将返回您提供的确切 SQL。这实际上是它返回的内容:

SELECT
`freeanswers`.*,
(SELECT `geodatas`.`districtCode`
    FROM `geodatas`
    WHERE (zipCode = clients.zipCode)
    GROUP BY `zipCode`
    LIMIT 1) AS `districtCode`,
`clients`.`zipCode`,
`clients`.`gender`,
`clients`.`startAge`,
`clients`.`endAge`,
`clients`.`mail`,
`clients`.`facebook`,
`clients`.`customerId`,
`clients`.`activityId`,
`surveys`.`name` AS `surveyName`,
`customers`.`companyName`,
`activities`.`name` AS `activityName`
FROM `freeAnswers`,
     `clients`,
     `surveys`,
     `customers`,
     `activities` AS `activities`
WHERE (freeAnswers.surveyId = surveys.id)
AND (surveys.customerId = customers.id)
AND (activities.id = surveys.activityId)
AND (clients.id = freeAnswers.clientId)
AND (customers.id = 1)
ORDER BY `activityName` ASC
LIMIT 10

所以这将起作用,但最终您将希望使用JOIN而不是指定大多数WHERE子句来重写它。

在处理子查询和Zend_Db_Select时,我发现在编写最终查询之前将每个子查询编写为自己的查询很容易,只需将子查询插入它们需要去的地方并Zend_Db处理其余部分。

希望有帮助。

于 2012-08-04T18:44:15.307 回答