mysql - "SELECT 1 UNION SELECT null UNION SELECT !1"的含义
迷茫
迷茫 2017-04-17 11:18:27
0
1
953
SELECT 1 UNION SELECT null UNION SELECT !1

的含义是什么?是选择空集的意思?

迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

reply all(1)
PHPzhong

Let’s talk about UNION first, which is used to combine the results of multiple different select statement executions into one result set and return it. Please refer to the official documentation. Understanding this, this statement can be split into 3 different select statements:

SELECT 1; //直接返回字面量1的值
SELECT null; //返回null本身的“值”。NULL是mysql中的一个常量,表示“没有值”,不是空字符串或0,按道理其本身也不应该有一个具体的值,mysql只是返回这样一个字符串表示而已(总不能什么都没发生啊)
SELECT !1   //逻辑非,非真即假,mysql中使用数字值1、0代表true和false,可用“SELECT TRUE, FALSE;”验证

As for how to write this SELECT statement, you can first read the official instructions. Except for at least one select_expr, other parts of the SELECT statement can be omitted, which means that this way of writing is legal first (SELECT can also be used to retrieve rows computed without reference to any table.). So what does it do? To put it simply, you can understand that MySQL is a calculator at this time. With the functions provided by MySQL, it can be used to do various calculations and return the final calculation result after successful execution. Example:

mysql> select 1+1; //加法
+-----+
| 1+1 |
+-----+
|   2 |
+-----+

mysql> select pow(2,3); //指数运算,2的3次方
+----------+
| pow(2,3) |
+----------+
|        8 |
+----------+

mysql> select !0;  //逻辑非
+----+
| !0 |
+----+
|  1 |
+----+

mysql> select 1 & 1;  //按位与
+-------+
| 1 & 1 |
+-------+
|     1 |
+-------+

mysql> set @a=10,@b=5;  //变量赋值后再运算
mysql> select @a+@b;
+-------+
| @a+@b |
+-------+
|    15 |
+-------+

//最后题主的语句
mysql> SELECT 1 UNION SELECT null UNION SELECT !1;
+------+
| 1    |
+------+
|    1 |
| NULL |
|    0 |
+------+
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template