如果找不到所有数据,则Mysql查询返回false

时间:2012-06-10 05:49:47

标签: php mysql codeigniter

我需要从几个表中获取一些用户信息,但是如果缺少某些内容,例如c.comp_title,则查询返回false。如何让它返回找到的任何数据?

    function one_edu($end_user, $one_id)
    {
    $query_str = "SELECT *
                FROM edu a
                JOIN user_profiles b ON a.user_id=b.user_id
                JOIN (SELECT c.user_id, GROUP_CONCAT(c.comp_title) as comp_title, GROUP_CONCAT(c.comp) as comp
                   FROM comp c
                   GROUP BY c.user_id) c ON a.user_id = c.user_id
                JOIN (SELECT s.user_id, GROUP_CONCAT(s.skill_title) as skill_title, GROUP_CONCAT(s.skill) as skill
                   FROM skills s
                   GROUP BY s.user_id) d ON a.user_id = d.user_id
                JOIN (SELECT t.user_id, GROUP_CONCAT(t.exp_title) as exp_title, GROUP_CONCAT(t.experience) as experience
                   FROM exp t
                   GROUP BY t.user_id) e ON a.user_id = e.user_id
                JOIN (SELECT e.user_id, GROUP_CONCAT(e.edu_title) as edu_title, GROUP_CONCAT(e.education) as education
                   FROM edu e
                   GROUP BY e.user_id) f ON a.user_id = f.user_id
                 WHERE a.user_id = ?";

            $query = $this->db->query($query_str, $end_user);

            if($query->num_rows() > 0)
                    {
                    foreach($query->result_array() as $stuff) {
                            $data[] = $stuff;
                    }
                            return $data;
                    }else{
                            return false;
                    }

    }//end one_edu

1 个答案:

答案 0 :(得分:1)

了解SQL joins。您需要使用外部联接 - 将JOIN替换为LEFT JOIN,只要您想要结果,即使要加入的表没有匹配的记录(或RIGHT JOIN,如果您想要结果,即使前面提到过表没有匹配的记录。)