0

在 Magento 中,我使用 Zend DDL 创建一个查询,将 group_concat() 表值放入一个名为“网站”的字段中。

查询工作正常,直到添加了 where 子句。

SELECT
  `main_table`.*,
  GROUP_CONCAT(website.website_id) AS `websites`
FROM
  `megamenu_entity` AS `main_table`
   LEFT JOIN `megamenu_website` AS `website`
   ON main_table.entity_id = website.megamenu_id
WHERE
  (websites = '1')
GROUP BY `main_table`.`entity_id`

这给出了错误:

SQLSTATE [42S22]:找不到列:1054 'where 子句'中的未知列'网站'

为什么是这样?谁能帮我解决这个问题?理想情况下在 Zend DDL 中。这就是我目前正在做的事情。

$collection->getSelect()->joinLeft(
  array('website' => $collection->getTable('megamenu/megamenu_website')),
  "main_table.entity_id = website.megamenu_id",
  array('websites' => new Zend_Db_Expr('GROUP_CONCAT(website.website_id)'))
)->group('main_table.entity_id');

谢谢

4

3 回答 3

1

您的WHERE子句将无法识别列别名。列别名可用于GROUP BY,ORDER BYHAVING子句。

更新了您的查询以使用原始列名,例如:

SELECT
  `main_table`.*,
  GROUP_CONCAT(website.website_id) AS `websites`
FROM
  `megamenu_entity` AS `main_table`
   LEFT JOIN `megamenu_website` AS `website`
   ON main_table.entity_id = website.megamenu_id
WHERE
  (website.website_id = '1')
GROUP BY `main_table`.`entity_id`
于 2013-02-07T10:56:57.290 回答
0

请尝试以下代码

SELECT * FROM 
(                     
SELECT
`main_table`.*,
GROUP_CONCAT(website.website_id) AS `websites`
FROM
`megamenu_entity` AS `main_table`
LEFT JOIN `megamenu_website` AS `website`
ON main_table.entity_id = website.megamenu_id ) E
WHERE
(E.websites = '1')
GROUP BY `main_table`.`entity_id`
于 2013-02-07T10:57:30.853 回答
0

试试这个查询..

SELECT
  `main_table`.*,
  GROUP_CONCAT(website.website_id) AS `websites`
FROM
  `megamenu_entity` AS `main_table`
   LEFT JOIN `megamenu_website` AS `website`
   ON main_table.entity_id = website.megamenu_id
WHERE
  ((GROUP_CONCAT(website.website_id)) = '1')
GROUP BY `main_table`.`entity_id`

您的查询不起作用,因为where在group之前执行。

于 2013-02-14T05:24:55.950 回答