且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

MySQL LEFT JOIN多个条件

更新时间:2021-12-10 21:45:14

只需将额外条件移到JOIN ON条件中,这样就不需要b的存在来返回结果

Just move the extra condition into the JOIN ON criteria, this way the existence of b is not required to return a result

SELECT a.* FROM a 
    LEFT JOIN b ON a.group_id=b.group_id AND b.user_id!=$_SESSION{['user_id']} 
    WHERE a.keyword LIKE '%".$keyword."%' 
    GROUP BY group_id