【发布时间】:2015-10-21 12:32:26
【问题描述】:
我目前正在构建一个小游戏来学习一些活动记录(codeigniter)和 php。所以我对这一切都很陌生。
当然,我在路上遇到了以下挑战。
挑战在于,我有两个表,User_Heroes 和 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->last_query();要获得最后一个查询,您必须在 phpmyadmin 或类似的 mysql 应用程序中使用查询来获得所需的问题。实际上,您的问题与 codeignter 无关,它与查询有关。
-
为了获得更好的解决方案您可以制作模拟情况的sql fiddle。
-
我知道它不是 codeigniter,我刚刚在如何正确构建这个查询(无论是在 Active Record 中还是在普通 MYSQL 中)方面遇到了困难
标签: php codeigniter join activerecord left-join