1

我正在构建类似系统的 facebook 线程。

我的问题是,当conv_fromidconv_toid与登录的用户 ID 相同时,它会打印错误的用户名。所以我正在寻找某种切换方法。不知道你/我能不能用sql case?

基本上问题是:

  • 当登录的userid等于conv_fromid时,需要根据conv_toid加入pub_users(不再是conv_fromid)
  • 如果登录的用户 ID 等于 conv_toid,则需要根据 conv_fromid 加入 pub_users。

我的请求 :

SELECT * FROM pub_conversations a
        INNER JOIN pub_users b
            ON a.conv_toid = b.user_id
        WHERE conv_fromid ='". get_uid() ."'
        OR conv_toid='". get_uid() ."'

所以输出最终应该是登录用户与之对话的所有用户的列表

http://sqlfiddle.com/#!2/feb21

4

2 回答 2

1
SELECT * FROM pub_conversations a
        INNER JOIN pub_users b
          ON a.conv_toid = b.user_id AND conv_fromid ='". get_uid() ."'
          OR a.conv_toid='". get_uid() ."' 

我不确定今天晚上我的大脑是否融化了:

SELECT * FROM pub_conversations a
        INNER JOIN pub_users b
          ON a.conv_toid = b.user_id AND conv_fromid ='". get_uid() ."'
          OR a.conv_toid='". get_uid() ."' AND conv_fromid != b.user_id


SELECT * FROM pub_conversations a
        INNER JOIN pub_users b
            ON b.user_id =(
CASE
 WHEN `conv_fromid`='". get_uid() ."' THEN `conv_toid`
 WHEN  `conv_toid`='". get_uid() ."' THEN `conv_fromid` ELSE -1 
END
)
于 2013-10-15T16:47:30.350 回答
0

Ended up using

SELECT * FROM pub_users a, pub_conversations b
    WHERE (CASE WHEN b.conv_fromid = ". get_uid() ." THEN b.conv_toid ELSE b.conv_fromid END) = a.user_id

Thanks anyway for your time :)

于 2013-10-16T19:47:54.410 回答