order_by 来自查询使不相关的查询失败

order_by from a query makes unrelated query fail

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

我在感兴趣的部分添加了一些评论 (//)。

$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 Error Number: 1054 Unknown column 'season'

in 'order clause' SELECT 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 

        
    

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

请记住,$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

要解决此问题,您应该在 update:

之前放置 order_bylimit
$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();

您的查询:

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

在此查询中 Mysql 未找到 game_resorts table 中的季节列。因此,如果您想使用季节进行排序,那么您需要同时使用 table 进行左连接。因为season栏发现进入了game_resort_cashtable.

这里是左连接查询:

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 数据库 我希望它对您有用。 谢谢