“SELECT 1 UNION SELECT null UNION SELECT ! The meaning of “1”

  mysql, question
SELECT 1 UNION SELECT null UNION SELECT !  one

The meaning is What? Do you mean to choose an empty set?

First, UNION is used to merge the results executed by several different select statements into a result set and return them. refer toOfficial documents. With this in mind, this statement can be split into 3 different select statements:

SELECT 1;  //Directly return the literal value of 1
 SELECT null;  //Returns the "value" of null itself.  NULL is a constant in mysql, which means "no value". It is not an empty string or 0. In principle, it should not have a specific value. mysql only returns such a string representation (it can't be that nothing has happened)
 SELECT !  1 // Logic!, either true or false. mysql uses numeric values 1 and 0 to represent true and false, and can use "SELECT TRUE, FALSE;"  Verification

Besides, the writing method of this SELECT statement can first be seen from the official explanation. Except for at least one select_expr, other parts of the SELECT statement can be omitted. That is to say, the writing method is first legal (SELECT CAN ALSO BE USED TO RETRIEVE ROWS COMPUTED WITH REFERENCE TO ANY TABLE). So what is the effect? In short, you can understand that MySQL is a calculator at this time, which can be used to make various calculations together with functions provided by MySQL, and return the final calculation results after successful execution. For 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 |
加------加