简体   繁体   English

Zend_Db_Select如何从子查询中选择(派生表)

[英]Zend_Db_Select how to select from a subquery (derived table)

how can I use Zend_Db_Select to directly select from a subquery (derived table)? 如何使用Zend_Db_Select直接从子查询(派生表)中进行选择?

See, I have 5 tables with the same structure, I want to get all rows from them, merge them and remove the duplicates. 看,我有5个具有相同结构的表,我想从中获取所有行,合并它们并删除重复项。 I am using UNION which removes duplicates automaticly. 我正在使用UNION自动删除重复项。 The problem is that I add a static column to each table before, so there is one column which is different => duplicatation occures. 问题是我之前为每个表添加了一个静态列,因此有一个不同的列=> duplicatation发生。

Here is my query so far: 这是我目前的查询:

SELECT `news_main`.*, 'main' as `category` 
FROM `news_main` 
UNION SELECT `news_politics`.*, 'politics' as `category` FROM `news_politics` 
UNION SELECT `news_society`.*, 'society' as `category` FROM `news_society` 
UNION SELECT `news_world`.*, 'world' as `category` FROM `news_world` 
UNION SELECT `news_business`.*, 'business' as `category` FROM `news_business` 
ORDER BY `date` DESC LIMIT 8

See how I add static values to the new column category ? 了解如何将静态值添加到新列category Now everything else is the same (there are duplicate rows), but since they are from different categories, UNION can't remove them. 现在其他一切都是相同的(有重复的行),但由于它们来自不同的类别,因此UNION无法删除它们。

So I thought I could SELECT all rows from this sub-query and group them to remove duplicates, like this: 所以我想我可以从这个子查询中SELECT所有行并将它们分组以删除重复项,如下所示:

SELECT * 
FROM (
    SELECT `news_main`.*, 'main' as `category` 
    FROM `news_main` 
    UNION SELECT `news_politics`.*, 'politics' as `category` FROM `news_politics`
    UNION SELECT `news_society`.*, 'society' as `category` FROM `news_society` 
    UNION SELECT `news_world`.*, 'world' as `category` FROM `news_world` 
    UNION SELECT `news_business`.*, 'business' as `category` FROM `news_business` 
    ORDER BY `date` DESC LIMIT 8
) as subtable 
GROUP BY `source` 
ORDER BY `date` DESC

I did run this in MySQL and it works perfectly.. the only problem is.... 我在MySQL中运行它并且它完美运行..唯一的问题是....

How do I execute this using Zend_Db_Select's fancy functions? 如何使用Zend_Db_Select的花哨功能执行此操作?

Thanks in advance! 提前致谢!

I'm not sure if you can use nesting selects in the from construct of Zend_Db_Select or if you should even be doing it that way, but an alternative solution would be to just get the db adapter and build the sql query manually. 我不确定你是否可以在Zend_Db_Select的from结构中使用嵌套选择,或者你是否应该这样做,但另一种解决方案是获取db适配器并手动构建sql查询。

$db = Zend_Db_Table::getDefaultAdapter();
$db->query("SELECT * 
    FROM (
        SELECT `news_main`.*, 'main' as `category` 
        FROM `news_main` 
        UNION SELECT `news_politics`.*, 'politics' as `category` FROM `news_politics`
        UNION SELECT `news_society`.*, 'society' as `category` FROM `news_society` 
        UNION SELECT `news_world`.*, 'world' as `category` FROM `news_world` 
        UNION SELECT `news_business`.*, 'business' as `category` FROM `news_business` 
        ORDER BY `date` DESC LIMIT 8
    ) as subtable 
    GROUP BY `source` 
    ORDER BY `date` DESC
");

related: Zend_Db_Table subquery 相关: Zend_Db_Table子查询

Just define a class to quote your subquery, and then you'll be able to add some more processing upon it in one place: 只需定义一个引用子查询的类,然后您就可以在一个地方添加更多处理:

class Acme_Db_Expr_Subquery extends Zend_Db_Expr {
    public function __toString()
    {
        return '( ' . $this->_expression . ' )';
    }
}

Then use it in FROM clause (my case, copypaste from actual application, working) or JOIN (speculating, did not try). 然后在FROM子句中使用它(我的情况下,来自实际应用程序的copypaste,工作)或JOIN(推测,没试过)。

$innerSelect = $dbTableSomeModel->select(true);
// Configure it, maybe kick around many layers of abstarction

$nestedSelect->from(
    array(
        'derived_alias' => new Acme_Db_Expr_Subquery( $innerSelect ),
    )
    ,array(
        'column_alias' => 'column_expression',
    )
);

From what I can tell from the source code for Zend_Db_Select, its from() method calls its _join() method, which has a case for when the first parameter to from() is a Zend_Db_Select object: http://framework.zend.com/svn/framework/standard/trunk/library/Zend/Db/Select.php 从我从Zend_Db_Select的源代码中可以看出,它的from()方法调用它的_join()方法,该方法有一个情况,即from()的第一个参数是Zend_Db_Select对象: http://framework.zend。 COM / SVN /框架/标准/主干/库/的Zend / DB / Select.php

} else if ($name instanceof Zend_Db_Expr|| $name instanceof Zend_Db_Select) {
        $tableName = $name;
        $correlationName = $this->_uniqueCorrelation('t');

If not, from() should support aliasing a subquery by wrapping it in parentheses to force it to be cast to a Zend_Db_Expr instance as in this example for joinRight(): Zend Framework: Zend_Db_Select - how to join custom subquery table? 如果没有,from()应该支持别名子查询,方法是将它包装在括号中以强制它转换为Zend_Db_Expr实例,如本例中的joinRight(): Zend Framework:Zend_Db_Select - 如何连接自定义子查询表?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM