Codeigniter UNION查询问题

时间:2013-11-25 08:07:13

标签: php mysql codeigniter

我在codeigniter中遇到UNION查询问题。它给出输出但是数组中的两列的名称相同。

$sql = "SELECT firstname FROM user_profile WHERE user_id ='".$id."'

 UNION SELECT email FROM user WHERE id='".$id."'";

$data = $this->db->query($sql);

print_r($data->result());

这是输出:

  Array
(

    [0] => stdClass Object
        (
            [firstname] => Dimpy
        )

    [1] => stdClass Object
        (
            [firstname] => sigsnusp@gmail.com
        )

)

2 个答案:

答案 0 :(得分:1)

$sql = "SELECT t1.firstname,t2.email FROM user_profile as t1 join user as t2 on t1.id = t2.id where WHERE t2.id='".$id."'";

答案 1 :(得分:0)

试试此代码段UNION query in Code Igniter

如果仍有问题,那么代码服务器还有另一个替代子查询库 在github CodeIgniter-Subqueries

我希望它对你有用!