我在使用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
这会给我带来这个错误:
我不知道我做错了什么。有人可以帮帮我吗?
所需的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的问题吗?
答案 0 :(得分:2)
您正在UNIONing两个SELECT查询,但这两个查询应该具有相同的列数。在第一个查询中,您选择以下字段:
`cont`. * , `cats`. *
在第二个查询中,您可以选择以下字段:
`cont`. *
答案 1 :(得分:0)
在
->join(array('contCat' => 'content_categories'), 'cont.id = contCat.id_content', array())
->join(array('cats' => 'categories'), 'contCat.id_category = cats.id')
你使用一个空数组用于contCat而没有用于猫,尝试向猫添加一个空数组,因为据我所知它将选择* all否则。
无论如何看看查询的外观。