我正在使用这两个语句来查询 BuddyPress 的友谊,但我想知道它们是否是一种更好的方法或组合这两个 MySQL 语句的方法:
//Statement for friendship initiator
$SQL1 = "SELECT wp_users.display_name AS'name',
wp_users.user_login AS 'fname',
wp_users.user_nicename AS 'surname'
FROM wp_users
INNER JOIN wp_bp_friends
ON wp_users.id = wp_bp_friends.friend_user_id
WHERE 1=1
AND wp_bp_friends.initiator_user_id = " . $user_id . "
AND wp_bp_friends.is_confirmed = 1";
//Statement for friendship non-initiator
$SQL2 = "SELECT wp_users.display_name AS 'name',
wp_users.user_login AS 'fname',
wp_users.user_nicename AS 'surname'
FROM wp_users
INNER JOIN wp_bp_friends
ON wp_users.id = wp_bp_friends.initiator_user_id
WHERE 1=1
AND wp_bp_friends.friend_user_id = " . $user_id . "
AND wp_bp_friends.is_confirmed = 1";
结果应该包含一个基于 $user_id 的友谊列表,无论他们是否发起了关系。