SQL连接问题和where子句

时间:2017-07-26 12:48:44

标签: mysql sql

我有以下消息表:

enter image description here

我正在做的是聊天记录,我尝试显示两个用户之间的所有消息,我尝试了不同的查询,但最终只收到了3条消息,这些消息是由E124' E124'但是我无法显示所有6条消息,包括那些接收到的消息' 1'

首次尝试

SELECT
  m.id,
  m.reciever,
  m.sender,
  m.message,
  m.TIME,
  m.reciver_read,
  r.userID,
  r.firstname AS senderName,
  r.lastname AS senderSurname,
  d.donorCode,
  d.firstname AS donorFname,
  d.lastname AS donorLname 
FROM
  messages m 
  INNER JOIN
    recipients r 
    ON r.userID = m.sender 
  INNER JOIN
    donors d 
    ON d.donorCode = m.reciever 
    AND m.reciever = 'E124' 
    AND r.userID = 1 
ORDER BY
  m.TIME ASC

产生:

enter image description here

第二次尝试

Select   m.id,
         m.reciever,
         m.sender,
         m.message,
         m.time,
         m.reciver_read,
         r.userID,
         r.firstname As senderName,
         r.lastname As senderSurname,
         d.donorCode,
         d.firstname As donorFname,
         d.lastname As donorLname
From     messages m
Inner Join recipients r
    On r.userID = m.sender
Inner Join donors d
    On d.donorCode = m.reciever
Join     recipients
    On m.reciever = recipients.userID
Where    m.reciever = 'E124'
         Or m.reciever = 1
            And r.userID = 1
Order By m.time Asc;

根本不显示任何结果

我想帮助我如何获得“' 1'收到的消息?在一个结果?

6 个答案:

答案 0 :(得分:3)

试试这个..希望它可以帮助你..

SELECT
  m.id,
  m.reciever,
  m.sender,
  m.message,
  m.TIME,
  m.reciver_read,
  r.userID,
  r.firstname AS senderName,
  r.lastname AS senderSurname,
  d.donorCode,
  d.firstname AS donorFname,
  d.lastname AS donorLname 
FROM
  messages m 
  INNER JOIN recipients r ON r.userID IN(m.sender,m.reciever) 

  INNER JOIN donors d ON d.donorCode IN(m.sender,m.reciever) 

WHERE m.reciever IN('E124','1') AND r.userID = 1 

ORDER BY
  m.TIME ASC

答案 1 :(得分:1)

也许:

FROM messages m 
JOIN recipients r 
  ON r.userID = m.sender 
 AND (m.reciever = 'E124' OR m.reciever = '1')
 AND (m.sender = 'E124' OR m.sender = '1')
LEFT JOIN donors d 
  ON d.donorCode = m.reciever 
 AND d.donorCode = 'E124'

答案 2 :(得分:1)

试试这个:

SELECT
  m.id,
  m.reciever,
  m.sender,
  m.message,
  m.TIME,
  m.reciver_read,
  r.userID,
  r.firstname AS senderName,
  r.lastname AS senderSurname,
  d.donorCode,
  d.firstname AS donorFname,
  d.lastname AS donorLname 
FROM
  messages m 
INNER JOIN
  recipients r ON r.userID = m.sender 
INNER JOIN
  donors d ON d.donorCode = m.reciever 
WHERE
  m.sender = '1' AND m.reciever = 'E124' OR
  m.sender = 'E124' AND m.reciever = '1'
ORDER BY
  m.TIME ASC

答案 3 :(得分:1)

您应该尝试以下查询。

SELECT
  m.id,
  m.reciever,
  m.sender,
  m.message,
  m.TIME,
  m.reciver_read,
  r.userID,
  r.firstname AS senderName,
  r.lastname AS senderSurname,
  d.donorCode,
  d.firstname AS donorFname,
  d.lastname AS donorLname 
FROM
  messages m 
  INNER JOIN recipients r ON r.userID = m.sender 
  INNER JOIN donors d ON d.donorCode = m.reciever 
    AND m.reciever in ('E124')
    AND m.sender in ('E124')
    AND r.userID = 1

Union All


SELECT
  m.id,
  m.reciever,
  m.sender,
  m.message,
  m.TIME,
  m.reciver_read,
  r.userID,
  r.firstname AS senderName,
  r.lastname AS senderSurname,
  d.donorCode,
  d.firstname AS donorFname,
  d.lastname AS donorLname 
FROM
  messages m 
  INNER JOIN recipients r ON r.userID = m.sender 
  INNER JOIN donors d ON d.donorCode = m.reciever 
    AND m.reciever in ('1')
    AND m.sender in ('1')
    AND r.userID = 1

答案 4 :(得分:0)

尝试使用石灰:

(m.reciever = 'E124'
     Or m.reciever = 1) 
        And r.userID = 1

答案 5 :(得分:0)

您可以使用IN子句。

SELECT
 m.id,m.reciever,m.sender,m.message,m.TIME,
 m.reciver_read,r.userID,r.firstname AS senderName,
 r.lastname AS senderSurname, d.donorCode,
 d.firstname AS donorFname, d.lastname AS donorLname 
FROM messages m 
INNER JOIN recipients r ON r.userID = m.sender 
INNER JOIN donors d ON d.donorCode = m.reciever 
AND m.reciever IN('E124','1') 
AND r.userID = 1
ORDER BY m.TIME ASC