【问题标题】:Zend DB and Conditional Case StatementsZend DB 和条件案例语句
【发布时间】:2011-01-18 09:41:18
【问题描述】:

我有一个 zend 应用程序,可以显示比赛结果的排行榜。基本过程是确定要包含的事件列表,然后将一组列动态添加到 SQL 查询中。我最初的实现相当垃圾,我想对其进行反思。我手动构建了一个 SQL 字符串,并在最后调用 db->fetch()。我知道 - 垃圾代码,但我仍在学习 zend 并且有截止日期。

public function league()
{
    ...

    $SQL = 'SELECT rr.runner AS runnerid, ru.firstname as firstname, ru.surname as surname, ';

    // get the list of events        
    foreach($events as $eventrow)
    {
        $eventTable = new Model_DbTable_Event();
        $event = $eventTable->find($eventrow->event)->current();
        // generate the conditional column
        $sum = $this->getEventSQL($event,$division->gender);
        // append to the SQL string (nasty) 
        $SQL = $SQL . $sum;
    }

    $SQL = $SQL . 'lrd.racesComplete, lrd.pointsTotal, c.name as company ';
    $SQL = $SQL . 'FROM raceresult rr ';
    $SQL = $SQL . 'JOIN runner ru ON rr.runner = ru.id ';
    $SQL = $SQL . 'LEFT JOIN company c ON c.id = ru.company ';
    $SQL = $SQL . 'JOIN race ra ON rr.race = ra.id ';
    ...
    ...

    $db = Zend_Db_Table::getDefaultAdapter();
    $this->view->leaguetable = $db->fetchAll($SQL);
}

// create the SUM sql statement for a specific event
// SUM(CASE rr.race WHEN 15 THEN rr.points ELSE NULL END) as Result1,
// SUM(CASE rr.race WHEN 16 THEN rr.points ELSE NULL END) as Result2,
function getEventSQL($event,$type)
{
    $eventTable = new Model_DbTable_Event();
    $sum_sql = 'SUM(CASE rr.race ';
    foreach($races as $race)
    {
        $sum_sql = $sum_sql . sprintf('WHEN %d',$race->id) . ' THEN rr.points ';
    }   
    $sum_sql = $sum_sql . sprintf('ELSE NULL END) as \'%s\',',$event->id);
    return $sum_sql;
}

我知道我需要使用 SQL SELECT/CASE 语句。

Conditional column for query based on other columns in MySQL

我想将此逻辑移至扩展 Zend_Db_Table_Abstract 的类,但我仍然不确定有条件地控制所选列的最佳方法是什么。我知道我可以添加多个 where() 子句,在这种情况下可以使用 column() 方法吗?

class Model_DbTable_League extends Zend_Db_Table_Abstract
{
    protected $_name = 'leaguerunnerdata';
    protected $_primary = 'Id';

    public function leagueTable($min,$max)
    {
        $sql = $this->select()
            ->setIntegrityCheck(false)
            ->from(array('l'=>'league'),array('col','col2','col3'))
            ->where('l.league = ?',1)
            ->where('r.standard > ?',$min)
            ->where('r.standard < ?',$max)
            ->order('l.pointsTotal DESC');
        return $this->fetchAll($sql);
    }

....

有什么想法/建议吗?

【问题讨论】:

  • 我也不喜欢调用选择实例 $sql,因为它真的不是 SQL。我称它为 $select,因为它就是这样。

标签: mysql zend-framework zend-db


【解决方案1】:

完美的答案

$select = $this->select()->setIntegrityCheck(false)

$select->from(array('l' => 'league'), array('leagueId'=> 'id', 'name','location','competitionName','eventManager','owner' )  )
                ->join(array('r' => 'whateverRTableIs'),
                         'r.id = l.id',
                             array())
                ->where('l.league = ?',1)
                ->where('r.standard > ?',$min)
                ->where('r.standard < ?',$max)
                ->order('l.pointsTotal DESC');
            return $this->fetchAll($select);

【讨论】:

    【解决方案2】:

    我发现我可以使用 'columns()' 方法为我的查询动态添加额外的字段,并使用 Zend_Db_Expr 正确格式化列的特定值。

    $select = $this->select()
    ->setIntegrityCheck(false)
    ->from(array('raceresult'=>'raceresult'),array())
    ->join(array('runner'=>'runner'),'runner.id=raceresult.runner',array('id','firstname','surname'));
        foreach($races as $taggedrace)
        {
            $select->columns(new Zend_Db_Expr(
                sprintf("SUM(CASE raceresult.race WHEN %d THEN raceresult.points ELSE NULL END) as %s",
                    $taggedrace->raceid,$taggedrace->tag)));
        }
    $select->join(array('race'=>'race'),'race.id = raceresult.race',array())
    ->join(array('event'=>'event'),'race.event = event.id',array())
    ->join(array('leagueevent'=>'leagueevent'),'leagueevent.event = event.id',array())
    ->join(array('leaguerunnerdata'=>'leaguerunnerdata'),
        'leaguerunnerdata.runner = raceresult.runner AND leaguerunnerdata.league=leagueevent.league',
        array('racesComplete','pointsTotal'))
    ->joinLeft(array('company'=>'company'), 'company.id = runner.company',array('name'))
    ->where( sprintf('leaguerunnerdata.standard BETWEEN %d AND %d ',$division->min,$division->max))
    ->where('runner.gender= ?',$division->gender)
    ->where('leagueevent.league = ?',$league)
    ->group(array('raceresult.runner','leaguerunnerdata.racesComplete','leaguerunnerdata.pointsTotal'))
    ->order(array('leaguerunnerdata.pointsTotal desc'));
    

    【讨论】:

      猜你喜欢
      • 2022-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多