Viceversa在mysql查询子句中

时间:2016-06-29 14:32:17

标签: mysql

我想根据两个条件

选择记录
$Attachments = $pstRootFolder.Items | ForEach-Object {
    $_.Attachments | Select-Object -ExpandProperty FileName
}

但是这个查询可以给我一个这样的记录:

  SELECT *
  FROM record
  WHERE FromPersonID in (1,2) AND
  ToPersonID in (1,2)

在id为2和3时,记录在FromPersonID和ToPersonID中具有相同的值

我想要的是它只会检查两个字段之间的反之亦然。

任何人都可以帮忙取消这个吗?

谢谢。

1 个答案:

答案 0 :(得分:1)

如果你想获得

     2             1              1
     3             2              2


SELECT *
  FROM record
  WHERE FromPersonID in (1,2) AND
  ToPersonID in (1,2) and FromPersonID=ToPersonID;

否则,如果您想要获得两个列具有不同价值的记录

SELECT *
      FROM record
      WHERE FromPersonID in (1,2) AND
      ToPersonID in (1,2) and FromPersonID!=ToPersonID;



SELECT * FROM TT;
+------+--------------+------------+
| ID   | FromPersonID | TOPERSONID |
+------+--------------+------------+
|    1 |            1 |          2 |
|    2 |            1 |          1 |
|    3 |            2 |          2 |
|    4 |            2 |          1 |
|    5 |            1 |          2 |
+------+--------------+------------+
5 rows in set (0.00 sec)

 SELECT *       FROM TT       WHERE FromPersonID in (1,2) AND  TOPERSONID in (1,2) and FromPersonID!=TOPERSONID;
+------+--------------+------------+
| ID   | FromPersonID | TOPERSONID |
+------+--------------+------------+
|    1 |            1 |          2 |
|    4 |            2 |          1 |
|    5 |            1 |          2 |
+------+--------------+------------+
3 rows in set (0.00 sec)