【发布时间】: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_resortFROM
game_resortsORDER BYseasonDESC 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'包含season。game_resorts不包含season,我从不向该表请求season。还是我?
标签: php mysql sql-order-by