0

我有两张桌子。您可以查看表格的链接:http ://sqlfiddle.com/#!2/feb71/1

我也得到了这个查询:

SELECT convID,Member1,Member2 
FROM tb_conversation WHERE Member1 = '1008' OR Member2 = '1008'

第一个查询是为表tb_conversation创建一个新列,该列将包含没有成员 1008 的 id 的列的记录。

例子

| CONVID | MEMBER1 | MEMBER2 | newColumn |
|    1   |   1008  |   1017  |    1017   |

第二个查询是连接 2 个表并检查这 2 个成员之间是否存在关系(有关系有 2 条记录 1fromID = 1008与其他记录toID = 1017关联,relStatus = 1其他记录fromID = 1017与1 关联)toID = 1008relStatus

实际上,我需要获取我的朋友列表中的成员(并且存在对话)和我的朋友列表中没有的成员(并且存在对话)。

有什么建议么?

4

1 回答 1

0

How about:

select tb0.*, tm0.relStatus from tb_conversation tb0 join
  tb_member_relation tm0 on 
    tm0.fromID = tb0.Member1 or
    tm0.toID = tb0.Member1 or
    tm0.fromID = tb0.Member2 or
    tm0.toID = tb0.Member2
  where tm0.fromID = 1008 or tm0.toID = 1008;

This should get you the conversations where 1008 is involved.

but

if you alter your table structure to eliminate the need for 'from' and 'to' and just use ends of a conversation you could simplify this a great deal.

于 2013-07-04T12:41:51.903 回答