与其他帖子相比,我有不同的情况。我有两个表,如:
订阅
现在,水果,零食,酱汁,午餐在子订单中列为类别表的ID。例如,如果Fruits is 1
然后从类别表中,水果名称是Apple
。我想从类别表列中获取其ID列在订阅表中的那些食物的名称:的 ITEMNAME
我正在使用codeigniter并尝试使用连接,但我没有看到任何值。
$this->db->select('*');
$this->db->from('subsscription');
$this->db->join('addschool','addschool.id=subsscription.schoolid');
$this->db->join('user','user.id=subsscription.studentid');
$this->db->join('category As u1','u1.id=subsscription.Fruits', 'left OUTER');
$this->db->join('category As u2','u2.id=subsscription.snack', 'left OUTER');
// $this->db->where('subsscription.snack >', '0');
$this->db->join('category As u3','u3.id=subsscription.sauce', 'left OUTER');
$this->db->join('category As u4','u4.id=subsscription.lunchdrink', 'left OUTER');
$this->db->join('category As u5','u5.id=subsscription.icypole', 'left OUTER');
$this->db->join('category As u6','u6.id=subsscription.lunch', 'left OUTER');
// $this->db->where('subsscription.Fruits','category.id');
// $this->db->group_by('subsscription.id');
$query = $this->db->get();
//echo $this->db->last_query();
if($query->num_rows() > 0)
{
return $query->result();
}
else
{
return false;
}
我的输出要求应该是:苹果,草莓牛奶,其中Apple的行ID是 1 ,草莓牛奶的行ID是 4 。
答案 0 :(得分:0)
我无法通过SELECT('')使其工作。我可能是错的,它经常发生,但我认为你不能这样做。以下对我有用。如果其他人有一个答案实际上只是抓住所有表中的所有内容,请随时添加它。
我也离开了学校的桌子,所以你必须重新学习。我用相关的信息创建了表格,并且不想制作那个。
$this->db->select('subsscription.id AS sub_id, user.name, u1.itemname AS fruit, u2.itemname AS snack, u3.itemname AS sauce, u4.itemname AS lunchdrink, u5.itemname AS icypole, u5.itemname AS lunch');
$this->db->from('subsscription');
$this->db->join('user','user.id=subsscription.studentid', 'JOIN');
$this->db->join('category As u1','subsscription.Fruits=u1.id', 'LEFT OUTER');
$this->db->join('category As u2','subsscription.snack=u2.id', 'LEFT OUTER');
$this->db->join('category As u3','subsscription.sauce=u3.id', 'LEFT OUTER');
$this->db->join('category As u4','subsscription.lunchdrink=u4.id', 'LEFT OUTER');
$this->db->join('category As u5','subsscription.icypole=u5.id', 'LEFT OUTER');
$this->db->join('category As u6','subsscription.lunch=u6.id', 'LEFT OUTER');
$query = $this->db->get();
if($query->num_rows() > 0) {
foreach($query->result() AS $row) {
print '<pre>' . print_r($row, TRUE) . '</pre>';
}
}
这个的输出是:
stdClass Object
(
[sub_id] => 1
[name] => Betty
[fruit] => Apple
[snack] =>
[sauce] =>
[lunchdrink] => Strawberry Milk
[icypole] =>
[lunch] =>
)
stdClass Object
(
[sub_id] => 2
[name] => John
[fruit] => Banana Bread
[snack] =>
[sauce] => Apple
[lunchdrink] =>
[icypole] => Strawberry Milk
[lunch] => Strawberry Milk
)
我显然使用随机值。