0

我正在为内部连接的 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

提前致谢

4

3 回答 3

3

我认为您需要的是对 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";
于 2012-11-29T14:37:45.240 回答
1

试试这个 ::

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.")
于 2012-11-29T14:29:48.963 回答
1
SELECT * FROM wallusers 
INNER JOIN wallfriends ON 
wallusers.mem_id = 
CASE WHEN 
wallfriends.mem_id_from =".$user_id." 
THEN wallfriends.mem_id_to 
ELSE wallfriends.mem_id_from
END
WHERE wallfriends.confirm=1
于 2012-11-29T14:35:10.697 回答