1

我需要从几个表中获取一些用户信息,但是如果缺少某些内容,例如 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
4

1 回答 1

1

了解SQL 连接。您需要使用外部连接 -即使要连接的表没有匹配的记录(或者如果您想要结果,即使前面提到的表没有匹配的记录)也需要替换JOIN为任何您想要的结果。LEFT JOINRIGHT JOIN

于 2012-06-10T05:59:52.867 回答