来自多个表的2个内连接的mysql查询

时间:2017-09-07 12:31:42

标签: php mysql

我有以下查询

SELECT name from login
(INNER JOIN connections on login.id = connections.receiver) AND (INNER JOIN connections on login.id = connections.sender)
WHERE (((connections.sender = '33') AND (connections.status = 'active'))
AND ((connections.receiver = '33') AND (connections.status = 'active')))

如果我的session_id(33)是接收者,并且如果我的session_id是发件人

,我试图获取所有发件人的姓名

应该怎么做?

3 个答案:

答案 0 :(得分:0)

您可以尝试使用IN()

    SELECT `name` from login
    WHERE `login`.`id` IN (SELECT `sender` FROM `connections` WHERE `sender` = '33' AND `status` = 'active')
    OR `login`.`id` IN (SELECT `receiver` FROM `connections` WHERE `receiver` = '33' AND `status` = 'active')

答案 1 :(得分:0)

INNER JOIN connections on login.id = connections.receiver AND login.id = connections.sender

尝试而不是这个

(INNER JOIN connections on login.id = connections.receiver) AND (INNER JOIN connections on login.id = connections.sender)

答案 2 :(得分:0)

此查询应显示名称以及找到名称的两种方案中的哪一种。

select distinct name, status from (
  select name , 'Receiver' status
  from login, connections 
  where login.id = connections.receiver
  and connections.receiver = '33'
  and connections.status = 'active'
  union
  SELECT name , 'Sender' status
  from login, connections 
  where login.id = connections.sender
  and connections.sender= '33'
  and connections.status = 'active'
)
相关问题