在Magento中,我使用Zend DDL创建查询,将group_concat()表值放入一个名为“sites”的字段中。
查询正常工作,直到添加了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');
由于
答案 0 :(得分:1)
您的WHERE
子句无法识别列别名。列别名可以在GROUP BY
,ORDER BY
和HAVING
子句中使用。
更新了您的查询以使用原始列名称,例如:
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`
答案 1 :(得分: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`
答案 2 :(得分: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`
您的查询无效,因为在群组之前 执行。