Article Directory
First, consider the following questions:
select 1=1; --返回什么?
select 1=1=1; --返回什么?
select -1=-1; --返回什么?
select 1=1; --返回什么?
select 5=5; --返回什么?
select 5=5=5; --返回什么?
select 5=5=1; --返回什么?
select 5=1=0; --返回什么?
It is very interesting to do a question today. The relevant link is here: I am the link https://blog.csdn.net/weixin_42845682/article/details/105264355
1. Several situations
1. Initial situation 1 = 1 = 1
First, there is the following table:
+------+------+------+------+------+------+
| i1 | n1 | i2 | n2 | i3 | n3 |
+------+------+------+------+------+------+
| 1 | 1 | 2 | 1 | 3 | 1 |
+------+------+------+------+------+------+
I want to find the value of n1 for n1 = n2 = n3.
Execute the following sql on this table:
select
n1
from logs
where n1 = n2 = n3
The query results are as follows:
+------+
| n1 |
+------+
| 1 |
+------+
2. Other cases 5 = 5 = 5
The values of n1, n2, and n3 above are all 1, assuming that they are changed to 5?
+------+------+------+------+------+------+
| i1 | n1 | i2 | n2 | i3 | n3 |
+------+------+------+------+------+------+
| 1 | 5 | 2 | 5 | 3 | 5 |
+------+------+------+------+------+------+
I want to find the value of n1 for n1 = n2 = n3.
Execute the following sql on this table:
select
n1
from logs
where n1 = n2 = n3
The query results are as follows:
Empty set (0.00 sec)
3. Other cases -1 = -1 = -1
Assuming the table is like this
+------+------+------+------+------+------+
| i1 | n1 | i2 | n2 | i3 | n3 |
+------+------+------+------+------+------+
| 1 | -1 | 2 | -1 | 3 | -1 |
+------+------+------+------+------+------+
I want to find the value of n1 for n1 = n2 = n3.
Execute the following sql on this table:
select
n1
from logs
where n1 = n2 = n3
The query results are as follows:
Empty set (0.00 sec)
Second, analyze
1. The correct sql
First of all, if you use the following sql, no matter what the values of n1, n2, n3 are, you can accurately find out.
select
n1
from logs
where n1 = n2
and n2 = n3
But I can't figure it out, why is n1 = n2 = n3 wrong?
2. Preliminary analysis
First declare: the logs table has been processed, and now there is only one data.
The following are the executed SQL and returned results:
mysql> select 1 from logs where 1=1;
+---+
| 1 |
+---+
| 1 |
+---+
1 row in set (0.00 sec)
mysql> select 1 from logs where 1=1=1;
+---+
| 1 |
+---+
| 1 |
+---+
1 row in set (0.00 sec)
You can see from the above two: 1 = 1 or 1 = 1 = 1 is no problem.
mysql> select 1 from logs where 5=5;
+---+
| 1 |
+---+
| 1 |
+---+
1 row in set (0.00 sec)
mysql> select 1 from logs where 5=5=5;
Empty set (0.00 sec)
mysql> select 1 from logs where -1=-1;
+---+
| 1 |
+---+
| 1 |
+---+
1 row in set (0.00 sec)
mysql> select 1 from logs where -1=-1=-1;
Empty set (0.00 sec)
As can be seen from the above, 5 = 5, -1 = -1 can be detected; but 5 = 5 = 5, -1 = -1 = -1 cannot be found.
3. Continue analysis
1). 1=1
The following are the executed SQL and returned results:
mysql> select 1=1;
+-----+
| 1=1 |
+-----+
| 1 |
+-----+
1 row in set (0.00 sec)
mysql> select 1=1=1;
+-------+
| 1=1=1 |
+-------+
| 1 |
+-------+
1 row in set (0.00 sec)
First, I always thought that 1 = 1 should return true, but returned 1. From here, I have an idea: the judgment condition in mysql (similar to n1 = n2), the return is not true or false, but the returned 1 and 0.
It can be seen that in sql, the result of 1 = 1 is 1 (assuming i), and the result of i = 1 is still 1.
2). Other situations
mysql> select 5=5;
+-----+
| 5=5 |
+-----+
| 1 |
+-----+
1 row in set (0.00 sec)
mysql> select 5=5=5;
+-------+
| 5=5=5 |
+-------+
| 0 |
+-------+
1 row in set (0.00 sec)
It can be seen that in sql, the result of 5 = 5 is 1 (assuming i), and the result of i = 5 becomes 0.
mysql> select -1=-1;
+-------+
| -1=-1 |
+-------+
| 1 |
+-------+
1 row in set (0.00 sec)
mysql> select -1=-1=-1;
+----------+
| -1=-1=-1 |
+----------+
| 0 |
+----------+
1 row in set (0.00 sec)
It can be seen that in sql, the result of -1 = -1 is 1 (assuming i), and the result of i = -1 becomes 0.
Verify again:
mysql> select 1!=1;
+------+
| 1!=1 |
+------+
| 0 |
+------+
1 row in set (0.00 sec)
Ok. . . . Where false should be returned, 0 is returned. It seems my idea is right.
3. Conclusion
1. Conclusion
About this sql:
select
n1
from logs
where n1 = n2 = n3
I always thought: in this sql, it should be the effect of n1 = n2 and n2 = n3; but in fact, it is n1 = n2, and then see if the value of n1 = n2 is equal to n3.
After several SQL judgments above, a judgment condition returns 1 or 0, corresponding to true or false.
In other words, n1 = n2, the result will only be 1 or 0.
2. Verification conclusion
Execute the following sql:
select 1=2=0;
If this is a code, it should return false. But if you look at the above conclusion, 1 = 2 returns 0, 0 = 0 returns 1, so it should be 1 to return. Look at the execution results:
mysql> select 1=2=0;
+-------+
| 1=2=0 |
+-------+
| 1 |
+-------+
1 row in set (0.00 sec)
It seems that my conclusion is correct. . .
4. Expand
According to the above sql, 1 = 2 = 0 returns 0. Then think about it, () should also take effect?
mysql> select 1=(2=0);
+---------+
| 1=(2=0) |
+---------+
| 0 |
+---------+
1 row in set (0.00 sec)
First execute 2 = 0, the result is 0; 0 = 1, return 0.
Verify again:
mysql> select 0=(2=0);
+---------+
| 0=(2=0) |
+---------+
| 1 |
+---------+
1 row in set (0.00 sec)
First execute 2 = 0, the result is 0; 0 = 0, return 1.
Well, my conclusion is correct, and () can increase the priority.
Five, post
Thanks to a gangster for pointing, I remembered it. I wrote the code in java, all written a == b && b == c, never written a == b == c ...
well, my brain is broken. . . From java, a == b must return a boolean, boolean! = The specific number is a normal behavior ...
but think about it, mysql returns 1 or 0 instead of true or false Is it a discovery? . . . Uh huh, count. . .
Six, after the post
1. The first verification
What is the relationship between 1 or 0 returned by mysql and true or false? …
mysql> select 1=true;
+--------+
| 1=true |
+--------+
| 1 |
+--------+
1 row in set (0.00 sec)
mysql> select 1=false;
+---------+
| 1=false |
+---------+
| 0 |
+---------+
1 row in set (0.00 sec)
mysql> select 0=false;
+---------+
| 0=false |
+---------+
| 1 |
+---------+
1 row in set (0.00 sec)
mysql> select 0=true;
+--------+
| 0=true |
+--------+
| 0 |
+--------+
1 row in set (0.00 sec)
2. Second verification
Ok. . . This is not a return value, it is a 1 or 0 defined by myself, try the return value.
mysql> select 1=1;
+-----+
| 1=1 |
+-----+
| 1 |
+-----+
1 row in set (0.00 sec)
mysql> select 1=1=true;
+----------+
| 1=1=true |
+----------+
| 1 |
+----------+
1 row in set (0.00 sec)
mysql> select 1=1=false;
+-----------+
| 1=1=false |
+-----------+
| 0 |
+-----------+
1 row in set (0.00 sec)
Try the value of 0
mysql> select 1=2;
+-----+
| 1=2 |
+-----+
| 0 |
+-----+
1 row in set (0.00 sec)
mysql> select 1=2=true;
+----------+
| 1=2=true |
+----------+
| 0 |
+----------+
1 row in set (0.00 sec)
mysql> select 1=2=false;
+-----------+
| 1=2=false |
+-----------+
| 1 |
+-----------+
1 row in set (0.00 sec)
3. Conclusion
The 1 or 0 returned by mysql, and the 1 or 0 defined by myself, are basically equal to true or false ...
I feel that I have moved myself for a long time ... ah ...