如何找到2个用户之间的公共列,但如果没有,则返回零结果,这意味着即使没有公共列也会返回结果。
SELECT u.usr_id, u.usr_fname, u.usr_lname, COUNT(ul.ulnk_lnk_id) AS common_links
FROM users AS u
LEFT JOIN user_links AS ul ON ul.ulnk_usr_id=u.usr_id
WHERE u.usr_id != $user_id
AND ul.ulnk_lnk_id IN
(SELECT l.ulnk_lnk_id FROM user_links AS l WHERE l.ulnk_usr_id=$user_id)
GROUP BY u.usr_id HAVING common_links >= 0 ORDER BY common_links DESC";
这将返回用户之间有一个或多个公共列的结果, 我需要返回一个包含0个常用列的结果......
答案 0 :(得分:0)
您的where子句排除了没有共同链接的任何用户。将ul.ulnk_lnk_id
检查移至ON
子句应该可以解决问题。
SELECT u.usr_id, u.usr_fname, u.usr_lname, COUNT(ul.ulnk_lnk_id) AS common_links
FROM users AS u
LEFT JOIN user_links AS ul ON (ul.ulnk_usr_id=u.usr_id AND ul.ulnk_lnk_id IN
(SELECT l.ulnk_lnk_id FROM user_links AS l WHERE l.ulnk_usr_id=$user_id) )
WHERE u.usr_id != $user_id
GROUP BY u.usr_id
ORDER BY common_links DESC
答案 1 :(得分:0)
试试这个:
SELECT u.usr_id, u.usr_fname, u.usr_lname, COUNT(ul.ulnk_lnk_id) AS common_links
FROM users AS u
LEFT JOIN user_links AS ul ON ul.ulnk_usr_id=u.usr_id
WHERE u.usr_id != $user_id
AND ul.ulnk_lnk_id IN
(SELECT l.ulnk_lnk_id FROM user_links AS l WHERE l.ulnk_usr_id=$user_id)
GROUP BY u.usr_id ORDER BY common_links DESC";