我正在努力对内部联接进行SQL查询。我想拥有它,以便它加入与$ user_id不同的字段。希望这可能更容易理解:

if ($user_id = wallfriends.mem_id_from) {
INNER JOIN wallfriends ON wallusers.mem_id = wallfriends.mem_id_to
} else if ($user_id = wallfriends.mem_id_to) {
INNER JOIN wallfriends ON wallusers.mem_id = wallfriends.mem_id_from
}


我当前的查询:

SELECT * FROM wallusers
INNER JOIN wallfriends ON wallusers.mem_id = wallfriends.mem_id_from
WHERE ((wallfriends.mem_id_from =".$user_id.") OR (wallfriends.mem_id_to =".$user_id.")) AND wallfriends.confirm=1


提前致谢

最佳答案

我认为您需要的是在sql字符串上进行三元比较

$sql = " SELECT * FROM wallusers
INNER JOIN wallfriends ON wallusers.mem_id = ".( $user_id == "wallfriends.mem_id_to"?"wallfriends.mem_id_from":"wallfriends.mem_id_to")."
WHERE wallfriends.confirm=1";

10-08 00:23