CI中的多个select语句

时间:2016-06-16 14:23:56

标签: php html mysql codeigniter

我有这行代码,

$val = self::$db->select('*')->select('a.id')->select("CONCAT(user_profile.firstname,' ',user_profile.middlename,' ',user_profile.lastname) AS user_name")->from('a')->join('user_profile', 'po.user_id = user_profile.id')->get()->result();

在此查询中,它将选择行中的所有数据。 选择了一列“modified_by”。 我想用它来获取相应的user_profile.firstname。

我试过这行代码但没有工作,

$val = self::$db->select('*')->select('a.id')->select('a.modified_by' as modifier)->select("CONCAT(user_profile.firstname,' ',user_profile.middlename,' ',user_profile.lastname) AS user_name")->select('user_profile.firstname where user_profile.id = modifier ')->from('a')->join('user_profile', 'po.user_id = user_profile.id')->get()->result();

1 个答案:

答案 0 :(得分:0)

要进一步了解@Keeleon的评论,您可以使用逗号组合SELECT语句,这样您的代码就完全合法了:

$this->db->select('*, a.id, a.modified_by as modifier, CONCAT(user_profile.firstname,' ',user_profile.middlename,' ',user_profile.lastname) AS user_name)-> .... ->get()

看起来你正在做一个子查询,这在使用codeigniter的默认函数时会变得非常毛茸茸 - 你可以使用这个答案来解决这个问题:https://stackoverflow.com/a/16303021/3629438

但它的要点是在组合之前将查询分开。

相关问题