Zend SQL 联合查询的问题

发布于 2024-10-20 14:20:32 字数 2508 浏览 6 评论 0原文

我在使用 Zend Framework 进行联合查询时遇到问题。

查询如下:

$localizedEvents = $db->select()
                                        ->from(array("cont" => "content"))
                                        ->where("cont.contentType = ?", 'event')
                                        ->where('cont.realm = ?', 'live')
                                        ->join(array('contCat' => 'content_categories'), 'cont.id = contCat.id_content', array())
                                        ->join(array('cats' => 'categories'), 'contCat.id_category = cats.id')
                                        ->where('cats.title like ?', "%$this->keyword%")
                                        ->distinct();
                                        
            $eventsQuery = $db->select()->from(array("cont" => "content"))
                                        ->where("cont.contentType = ?", 'event')
                                        ->where('cont.content LIKE ? ', "%$termEncoded%")
                                        ->where('cont.realm = ?', 'live');
            
            $finalQuery = $db->select()->union(array($localizedEvents, $eventsQuery))->order('cont.publishDate DESC');

生成的查询如下:

SELECT  `cont`. * ,  `cats`. * 
FROM  `content` AS  `cont` 
INNER JOIN  `content_categories` AS  `contCat` ON cont.id = contCat.id_content
INNER JOIN  `categories` AS  `cats` ON contCat.id_category = cats.id
WHERE (

cont.contentType =  'event'
)
AND (
cont.realm =  'live'
)
AND (
cats.title LIKE  '%conferência%'
)
UNION SELECT  `cont`. * 
FROM  `content` AS  `cont` 
WHERE (
cont.contentType =  'event'
)
AND (
cont.content LIKE  '%confer\\\\u00eancia%'
)
AND (
cont.realm =  'live'
)
ORDER BY  `cont`.`publishDate` DESC 
LIMIT 0 , 30

这将返回此错误:

1222 - 使用的 SELECT 语句具有不同数量的列

我不知道我做错了什么。有人可以帮我吗?

所需的 SQL 查询应该是:

SELECT  `cont`. * 
FROM  `content` AS  `cont` 
INNER JOIN  `content_categories` AS  `contCat` ON cont.id = contCat.id_content
INNER JOIN  `categories` AS  `cats` ON contCat.id_category = cats.id
WHERE (
cont.contentType =  'event'
)
AND (
cont.realm =  'live'
)
AND (
cats.title LIKE  '%conferência%'
)
UNION SELECT  `cont`. * 
FROM  `content` AS  `cont` 
WHERE (
cont.contentType =  'event'
)
AND (
cont.content LIKE  '%confer\\\\u00eancia%'
)
AND (
cont.realm =  'live'
)
LIMIT 0 , 30

有人可以帮助我如何将此查询转换为 Zend 吗?

I'm having trouble making a Union query using the Zend Framework.

The queries are these:

$localizedEvents = $db->select()
                                        ->from(array("cont" => "content"))
                                        ->where("cont.contentType = ?", 'event')
                                        ->where('cont.realm = ?', 'live')
                                        ->join(array('contCat' => 'content_categories'), 'cont.id = contCat.id_content', array())
                                        ->join(array('cats' => 'categories'), 'contCat.id_category = cats.id')
                                        ->where('cats.title like ?', "%$this->keyword%")
                                        ->distinct();
                                        
            $eventsQuery = $db->select()->from(array("cont" => "content"))
                                        ->where("cont.contentType = ?", 'event')
                                        ->where('cont.content LIKE ? ', "%$termEncoded%")
                                        ->where('cont.realm = ?', 'live');
            
            $finalQuery = $db->select()->union(array($localizedEvents, $eventsQuery))->order('cont.publishDate DESC');

the generated query is as follows:

SELECT  `cont`. * ,  `cats`. * 
FROM  `content` AS  `cont` 
INNER JOIN  `content_categories` AS  `contCat` ON cont.id = contCat.id_content
INNER JOIN  `categories` AS  `cats` ON contCat.id_category = cats.id
WHERE (

cont.contentType =  'event'
)
AND (
cont.realm =  'live'
)
AND (
cats.title LIKE  '%conferência%'
)
UNION SELECT  `cont`. * 
FROM  `content` AS  `cont` 
WHERE (
cont.contentType =  'event'
)
AND (
cont.content LIKE  '%confer\\\\u00eancia%'
)
AND (
cont.realm =  'live'
)
ORDER BY  `cont`.`publishDate` DESC 
LIMIT 0 , 30

This returns me this error:

1222 - The used SELECT statements have a different number of columns

I have no idea what I'm doing wrong. Can somebody help me please?

The desired SQL Query should be:

SELECT  `cont`. * 
FROM  `content` AS  `cont` 
INNER JOIN  `content_categories` AS  `contCat` ON cont.id = contCat.id_content
INNER JOIN  `categories` AS  `cats` ON contCat.id_category = cats.id
WHERE (
cont.contentType =  'event'
)
AND (
cont.realm =  'live'
)
AND (
cats.title LIKE  '%conferência%'
)
UNION SELECT  `cont`. * 
FROM  `content` AS  `cont` 
WHERE (
cont.contentType =  'event'
)
AND (
cont.content LIKE  '%confer\\\\u00eancia%'
)
AND (
cont.realm =  'live'
)
LIMIT 0 , 30

Can somebody help me how to turn this query into Zend?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

撩心不撩汉 2024-10-27 14:20:32

您正在合并两个 SELECT 查询,但这两个查询应该具有相同的列数。在第一个查询中,您选择以下字段:

`cont`. * ,  `cats`. *

在第二个查询中,您选择以下字段:

`cont`. *

You're UNIONing two SELECT queries, but both those queries are supposed to have the same column count. In the first query, you select the following fields:

`cont`. * ,  `cats`. *

In the second query you select these fields:

`cont`. *
病女 2024-10-27 14:20:32

->join(array('contCat' => 'content_categories'), 'cont.id = contCat.id_content', array())
->join(array('cats' => 'categories'), 'contCat.id_category = cats.id')

You use an empty array for contCat and none for cats 时,请尝试向 cats 添加一个空数组,因为据我所知,否则它会选择 * all 。

无论如何,看看查询看起来如何。

On

->join(array('contCat' => 'content_categories'), 'cont.id = contCat.id_content', array())
->join(array('cats' => 'categories'), 'contCat.id_category = cats.id')

You use an empty array for contCat and none for cats, try adding an empty array to cats as well, because as far as i know it will select * all otherwise.

Anyways see how the query looks then.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文