【问题标题】:order_by from a query makes unrelated query fail查询中的 order_by 使不相关的查询失败
【发布时间】:2017-05-08 12:56:57
【问题描述】:

我遇到了一个问题,我无法在我的一个 MYSQL 查询中使用order_by,因为它使另一个无法工作。我不明白的是,两个查询都是不相关的,更改第一个查询不应该影响第二个查询。 你能明白为什么order_by 会影响list_all_resorts 吗?

我在有趣的部分添加了一些 cmets (//)。

$shift_level = $this->shift_previous_day($list_all_resorts, 'level');

protected function shift_previous_day($list_all_resorts, $type) {
    foreach ($list_all_resorts->result() as $list_all_resorts_Array){
        $shift_DB = $this->shift_previous_day_DB($list_all_resorts_Array->id_resort, $type);
    }
    echo $shift_previous_day_DB;
} 
    
protected function shift_previous_day_DB($id_resort, $type){
    $this->db->trans_start();
    $this->db->set($type.'_d134', $type.'_d133', FALSE);
    $this->db->set($type.'_d1', $type.'_d0', FALSE);
    $this->db->set($type.'_d0', '0', FALSE);
    $this->db->where('id_resort', $id_resort);
    $this->db->update('game_resort_'.$type); 
    $this->db->order_by('season', 'DESC');  // If I remove this line there is no error thrown
    $this->db->limit('1'); 
    $this->db->trans_complete();
    if ($this->db->trans_status() === FALSE){
        return log_message();
    }
    else {
        $result = 'all OK';
        return $result;
    }  
}

$add_cash_to_history = $this->add_todays_stat_to_history('cash'); 
 
//This is where list_all_resorts will fail. Before that it works fine
protected function add_todays_stat_to_history($data_type) {
    $info_message = '';
    $list_all_resorts = $this->list_all_resorts();  // The is where list_all_resorts is called
    foreach ($list_all_resorts->result() as $row_list_all_resorts){
        //do some stuff
    }
    return $info_message;
}
    
protected function list_all_resorts(){  
    $this->db->select('id_resort'); // This is where the error points to
    $this->db->from('game_resorts');
    return $this->db->get();
}

我得到的错误是:

A Database Error Occurred 错误号:1054 Unknown column 'season'

在“顺序子句”中选择game_resorts.id_resort FROM

game_resorts ORDER BY season DESC LIMIT 1

更新:

describe game_resorts:
id_resort   int(11) NO  PRI     auto_increment  
resort_name varchar(45) NO              
resort_country  varchar(45) YES             
resort_description  varchar(256)    YES             
id_player   int(11) YES MUL 

describe `game_resort_cash`:
id  int(11) NO  PRI         
id_resort   int(11) NO  MUL         
season  int(3)  NO      1       
cash_d0 int(11) YES             
cash_d1 int(11) YES                         
cash_d134   int(11) YES 

        
    

【问题讨论】:

  • 从您的错误消息中,MySQL 认为 game_resorts 表中不存在该列 season。您确定 game_resorts 表中存在列 season 吗?仔细检查两者的拼写?
  • 能否提供describe game_resorts;的输出?
  • list_all_resorts 不处理表'game_resort_'.$type,类似于'game_resort_cash'。只有'game_resort_cash' 包含seasongame_resorts 不包含 season,我从不向该表请求 season。还是我?

标签: php mysql sql-order-by


【解决方案1】:

现在您说的是 SELECT game_resorts.id_resort ,这将是 game_resorts 表中的 id_resort col。您不是在告诉它查找所有列,而是查找特定列。如果您不想全选,请执行SELECT game_resorts.id_resort, game_resorts.season... 换句话说:$this->db->select('id_resort'); 也应该调用季节列。更新那个人。

【讨论】:

  • season 不在game_resorts 中,我没有在此查询中选择它。至少我不这么认为……?
【解决方案2】:

请记住,$this->db 在两个函数调用中都是同一个对象,所以让我们看看它接收到的调用顺序:

//From shift_previous_day_DB
$this->db->trans_start();
$this->db->set($type.'_d134', $type.'_d133', FALSE);
$this->db->set($type.'_d1', $type.'_d0', FALSE);
$this->db->set($type.'_d0', '0', FALSE);
$this->db->where('id_resort', $id_resort);
$this->db->update('game_resort_'.$type); 
$this->db->order_by('season', 'DESC');
$this->db->limit('1'); 
$this->db->trans_complete();
//From list_all_resorts
$this->db->select('id_resort');
$this->db->from('game_resorts');
$this->db->get();

有些方法只是为执行下一个查询设置db 对象,以及执行查询和重置对象的方法。 updateget 属于第二组,而 setwhereorder_bylimitselectfrom 属于第一组。 trans_starttrans_complete 独立于它们工作。

由于对order_bylimit 的调用在对update 的调用之后,它们会影响下一条语句,即get。可以在错误信息中看到:生成的语句中不仅有ORDER BY season DESC,而且还有LIMIT 1

要解决此问题,您应该将order_bylimit 放在update 之前:

$this->db->trans_start();
$this->db->set($type.'_d134', $type.'_d133', FALSE);
$this->db->set($type.'_d1', $type.'_d0', FALSE);
$this->db->set($type.'_d0', '0', FALSE);
$this->db->where('id_resort', $id_resort);
$this->db->order_by('season', 'DESC');
$this->db->limit('1'); 
$this->db->update('game_resort_'.$type); 
$this->db->trans_complete();

【讨论】:

  • 太棒了,成功了!我怀疑是这样的,但我认为trans_complete 会重置一切
【解决方案3】:

您的查询:

SELECT game_resorts.id_resort FROM game_resorts ORDER BY season DESC LIMIT 1

在这个查询Mysql 中没有找到game_resorts 表中的season 列。因此,如果您想使用季节进行排序,那么您需要将两个表都左连接。因为在 game_resort_cash 表中找到了赛季列。

这是左连接查询:

select t1.*,t2.* from game_resorts t1
left join game_resort_cash t2
on(t1.id_resort=t2.id_resort) order by t2.season desc limit 1

将此查询运行到您的 Mysql 数据库中,希望它对您有用。 谢谢。

【讨论】:

    猜你喜欢
    • 2010-09-30
    • 1970-01-01
    • 2023-03-26
    • 1970-01-01
    • 2015-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多