0

好的,所以我遇到了以下问题。我正在尝试使用两个子查询执行单个 select 语句。我需要它做的是以下几乎..

SELECT * FROM messages WHERE满足以下条件。

条件一:

 SELECT id 
 FROM messages 
 WHERE `receiver` = 123510 && sender= 123457 && status != 3;

条件2:

 SELECT id 
 FROM messages 
 WHERE receiver = 123457 && sender = 123510 && status !=4;

我尝试执行以下操作:

SELECT * 
FROM messages 
WHERE id IN (
                 (
                     SELECT id 
                     FROM messages 
                     WHERE `receiver` = 123510 && sender= 123457 && status != 3
                 ),
                 (
                     SELECT id 
                     FROM messages 
                     WHERE receiver = 123457 && sender = 123510 && status !=4)
                  )  

但是当我这样做时,mysql返回子查询返回超过1行......

关于如何解决这个问题的任何建议?

4

2 回答 2

3

我认为这就是你要找的:

SELECT *
FROM messages
WHERE (receiver = 123510 AND sender = 123457 AND status <> 3) OR
      (receiver = 123457 AND sender = 123510 AND status <> 4);
于 2012-08-01T06:06:49.037 回答
0

您必须使用联合分隔每个查询,或者将其作为两个单独的 IN 使用 OR 分隔。

SELECT * 
FROM messages 
WHERE id IN (
          SELECT id
          FROM messages
          WHERE `receiver` = 123510 and 
                sender= 123457 and 
                status != 3) or
          UNION
          SELECT id
          FROM messages 
          WHERE receiver = 123457 and 
          sender = 123510 and
          status !=4)

但实际上,这样做会好得多。

SELECT * 
FROM messages 
WHERE (`receiver` = 123510 and 
       sender = 123457 and 
       status != 3) or
      (receiver = 123457 and 
       sender = 123510 and
       status !=4)
于 2012-08-01T06:12:03.217 回答