一、any,some,all

any,some,all 是 MySQL 当中的逻辑运算符,作用是将子查询返回的单列值的集合与查询的单个值作比较。

any,all,some 前面需跟比较运算符(>, >=, <, <=, !=, =,但是这里的单个使用 = 号的情况只有子集的数据是单个值或者子集数据全部相同才能符合情况,如果子集是多个不同的就不满足,根据实际情况使用)

mysql> select * from chat_communication;
+----+--------+------------------+------+------------------+--------------+------------+--------+--------+------+
| id | fromid | fromname         | toid | toname           | content      | time       | shopid | isread | type |
+----+--------+------------------+------+------------------+--------------+------------+--------+--------+------+
|  1 |     85 | Love violet life |   87 | 大金             | 你好         | 1517888705 |   NULL |      1 |    1 |
|  2 |     87 | 大金             |   85 | Love violet life | 你也好       | 1517888712 |   NULL |      1 |    1 |
|  3 |     89 | 雨薇             |   87 | 大金             | 你在干嘛     | 1517888760 |   NULL |      1 |    1 |
|  4 |     85 | Love violet life |   87 | 大金             | 你还在吗     | 1517888797 |   NULL |      0 |    1 |
|  5 |     85 | Love violet life |   87 | 大金             | 订单         | 1517888872 |   NULL |      0 |    1 |
+----+--------+------------------+------+------------------+--------------+------------+--------+--------+------+
5 rows in set (0.00 sec)

all 逻辑运算符

例如:select …from …where c > all(…)

表示 c 列中的值必须要大于子查询集的每一个值,即必须要大于子查询集的最大值;

如果是小于号即小于子查询集的最小值。同理可以推出其它的比较运算符的情况。

示例:列中的值必须要大于子查询集的每一个值
查询 fromid 大于 fromid = 85 所有数据。

SELECT
	*
FROM
	chat_communication
WHERE
	fromid > ALL (
		SELECT
			fromid
		FROM
			chat_communication
		WHERE
			fromid = 85
	);
+----+--------+----------+------+------------------+--------------+------------+--------+--------+------+
| id | fromid | fromname | toid | toname           | content      | time       | shopid | isread | type |
+----+--------+----------+------+------------------+--------------+------------+--------+--------+------+
|  2 |     87 | 大金     |   85 | Love violet life | 你也好       | 1517888712 |   NULL |      1 |    1 |
|  3 |     89 | 雨薇     |   87 | 大金             | 你在干嘛     | 1517888760 |   NULL |      1 |    1 |
+----+--------+----------+------+------------------+--------------+------------+--------+--------+------+
2 rows in set (0.01 sec)

any 和 some 运算符

any 和 some 作用是一样的。

例如

select…from…where c > any(…)
或者
select …from …where c > some(…)

表示 c 列中的值要大于子查询中的任意一个值,即必须要大于子查询集中的最小值。同理可以推出其它的比较运算符的情况。

示例: 查询 fromid = 87 至少大于 fromid = 85 的数据

SELECT
	*
FROM
	chat_communication
WHERE
	fromid = 87
AND fromid > ANY (
	SELECT
		fromid
	FROM
		chat_communication
	WHERE
		fromid = 85
);
+----+--------+----------+------+------------------+-----------+------------+--------+--------+------+
| id | fromid | fromname | toid | toname           | content   | time       | shopid | isread | type |
+----+--------+----------+------+------------------+-----------+------------+--------+--------+------+
|  2 |     87 | 大金     |   85 | Love violet life | 你也好    | 1517888712 |   NULL |      1 |    1 |
+----+--------+----------+------+------------------+-----------+------------+--------+--------+------+
1 row in set (0.00 sec)
SELECT
	*
FROM
	chat_communication
WHERE
	fromid = 87
AND fromid > SOME (
	SELECT
		fromid
	FROM
		chat_communication
	WHERE
		fromid = 85
);
+----+--------+----------+------+------------------+-----------+------------+--------+--------+------+
| id | fromid | fromname | toid | toname           | content   | time       | shopid | isread | type |
+----+--------+----------+------+------------------+-----------+------------+--------+--------+------+
|  2 |     87 | 大金     |   85 | Love violet life | 你也好    | 1517888712 |   NULL |      1 |    1 |
+----+--------+----------+------+------------------+-----------+------------+--------+--------+------+
1 row in set (0.00 sec)
11-06 14:49