0

顺便说一句,我现在无法重写当前的数据库架构,但这与我遇到的问题无关,所以请忽略表结构:D

我正在运行这个数据库查询:

my $rs = $dbx->resultset('Result')->search(
           {
             'result_hnd'         => 16078055,
             'seasons.outdoor'    => 'venue.outdoors',
             'seasons.start_date' => { '<=' => 'meet.date_end'   },
             'seasons.end_date'   => { '>=' => 'meet.date_begin' },
           },
           {
             'join' => [
               {
                 'team' => {
                   'league_teams' => {
                     'league' => 'seasons',
                   },
                 },
               },
               {
                 'meet' => 'venue'
               },
             ],
             '+select' => ['seasons.season_hnd','seasons.name','seasons.start_date','seasons.end_date','meet.date_begin','meet.date_end'],
             '+as'     => ['season_hnd','season_name','s_start','s_end','m_start','m_end'],
             columns   => ['result_hnd'],
             group_by  => ['seasons.season_hnd'],
           }
         );

当我运行它时,我没有得到任何结果。启用 DBIC_TRACE 后,我将生成的 SQL 视为:

SELECT me.result_hnd, seasons.season_hnd, seasons.name, seasons.start_date, seasons.end_date, meet.date_begin, meet.date_end FROM track.result me JOIN track.team team ON team.team_hnd = me.team_hnd LEFT JOIN track.league_team league_teams ON league_teams.team_hnd = team.team_hnd LEFT JOIN track.league league ON league.league_hnd = league_teams.league_hnd LEFT JOIN track.season seasons ON seasons.league_hnd = league.league_hnd OR seasons.league_hnd = league.parent_league_hnd JOIN track.meet meet ON meet.meet_hnd = me.meet_hnd JOIN track.venue venue ON venue.venue_hnd = meet.venue_hnd WHERE ( ( result_hnd = ? AND seasons.end_date >= ? AND seasons.outdoor = ? AND seasons.start_date <= ? ) ) GROUP BY seasons.season_hnd: '16078055', 'meet.date_begin', 'venue.outdoors', 'meet.date_end'

当我将此语句复制并粘贴到我的 MYSQL 客户端(并插入占位符)时,如下所示:

SELECT me.result_hnd, seasons.season_hnd, seasons.name, seasons.start_date, seasons.end_date, meet.date_begin, meet.date_end 
  FROM track.result me 
      JOIN track.team team ON team.team_hnd = me.team_hnd 
      LEFT JOIN track.league_team league_teams ON league_teams.team_hnd = team.team_hnd 
      LEFT JOIN track.league league ON league.league_hnd = league_teams.league_hnd 
      LEFT JOIN track.season seasons ON seasons.league_hnd = league.league_hnd OR seasons.league_hnd = league.parent_league_hnd 
      JOIN track.meet meet ON meet.meet_hnd = me.meet_hnd 
      JOIN track.venue venue ON venue.venue_hnd = meet.venue_hnd 
WHERE ( ( result_hnd = 16078055 AND seasons.end_date >= meet.date_begin AND seasons.outdoor = venue.outdoors AND seasons.start_date <= meet.date_end ) ) 
  GROUP BY season_hnd;

我得到了我期望的确切结果(7 条记录)。

这真的很奇怪。出于所有意图和目的,这不是完全相同的查询吗?我在调试中遗漏了什么吗?还是在 DBIx::Class::ResultSet 层发生了其他没有被转储的事情?

4

2 回答 2

4

要告诉 SQL::Abstract 右边的值实际上是一个标识符,您可以执行以下操作(如docs中所述):

   {
     'result_hnd'         => 16078055,
     'seasons.outdoor'    => { -ident => 'venue.outdoors' },
     'seasons.start_date' => { '<=' => { -ident => 'meet.date_end' }   },
     'seasons.end_date'   => { '>=' => { -ident => 'meet.date_begin' } },
   },
于 2012-10-09T19:25:37.167 回答
1

出于所有意图和目的,这不是完全相同的查询吗?

出于所有意图和目的,...)

不,在日志中,你有相当于

result_hnd = '16078055'
AND seasons.end_date >= 'meet.date_begin'
AND seasons.outdoor = 'venue.outdoors'
AND seasons.start_date <= 'meet.date_end'

或者可能

result_hnd = 16078055
AND seasons.end_date >= 'meet.date_begin'
AND seasons.outdoor = 'venue.outdoors'
AND seasons.start_date <= 'meet.date_end'

在您的尝试中,您使用了

result_hnd = 16078055
AND seasons.end_date >= meet.date_begin
AND seasons.outdoor = venue.outdoors
AND seasons.start_date <= meet.date_end

抱歉,我没有适合你的解决方案。

于 2012-10-09T18:45:43.227 回答