gpt4 book ai didi

php - 如果没有找到所有数据,Mysql 查询返回 false

转载 作者:行者123 更新时间:2023-11-29 00:44:38 27 4
gpt4 key购买 nike

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

最佳答案

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

关于php - 如果没有找到所有数据,Mysql 查询返回 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10966641/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com