[英]Codeigniter, active record, join
我目前正在构建一个小游戏,以学习一些活动记录(codeigniter)和php。 因此,我对所有这些都是新手。
当然,我在旅途中遇到了以下挑战。
面临的挑战是,我有两个表User_Heroes和Heroes 。 英雄表包含用户在玩游戏时可以选择的所有英雄(属于他的派系),而User_Heroes是用户已经招募到其服务中的英雄表。
我想做的事情如下,我想做一个查询,返回一个当前用户没有的英雄列表。
为了简单地获得它们,我会这样做
public function get_faction_heroes($sUser, $hero_faction){
$oQuery = $this -> db
-> select(' id,
hero_name,
hero_faction,
hero_type,
hero_cost,
hero_maintenance,
hero_allowed')
-> like('hero_faction',$hero_faction)
-> get('heroes')
-> result();
return $oQuery;
}
目前,我已经建立了以下查询,但是显然这是错误的,因为它没有返回我期望返回的列表。
public function get_faction_heroes($sUser, $hero_faction){
$oQuery = $this -> db
-> select(' h.id,
h.hero_name,
h.hero_faction,
h.hero_type,
h.hero_cost,
h.hero_maintenance,
h.hero_allowed')
-> like('h.hero_faction',$hero_faction)
-> where_not_in('u.hero_id', $sUser)
-> where('h.hero_allowed >', 1)
-> join('user_heroes u', 'u.hero_id = h.id', 'left')
-> get('heroes h')
-> result();
return $oQuery;
}
您可能会注意到,某些英雄被允许多次招募(hero_allowed> 1)。
也许为了进一步清理表,我在当前表中添加了两个图像
$this->db->select('*')->from('Heroes');
$this->db->where('`id` NOT IN (SELECT `hero_id` FROM `User_Heroes`)', NULL, FALSE);
$query=$this->db->get();
return $query->result();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.