Query Results of MySQL in operations are displayed in the in set order _ MySQL
Source: Internet
Author: User
The query results of the MySQL Query in operation are displayed in the in set order. MySQL queries the in operation. the query results are displayed in the in set order.
Select * from test where id in (3, 1, 5) order by find_in_set (id, '3, 1, 5 ');
Select * from test where id in (3, 1, 5) order by substring_index ('3, 1, 2 ', id, 1 );
Occasionally seen... Some people may have noticed it, but I did not know it before.
SQL: select * from table where id IN (3, 6, 9, 1, 2, 5, 8, 7 );
After such a situation is obtained, the IDS are actually sorted by 1, 2, 3, 4, 5, 6, 7, 8, 9, but what if we really want to sort IN order? Can SQL be completed? Do you need to get it back before foreach? In fact, mysql has this method.
SQL: select * from table where id IN (,) order by field (id );
The order is the specified order .... This is something that I have never used before and can be seen occasionally, so I recorded it. One is to take a note, and the other is to show it to more people.
Processing NULL values by the not in statement IN MySQL
Mysql> select count (name) from cve where name not in ('cve-2017-0001 ', 'cve-2017-0002 ');
+ ------------- +
| Count (name) |
+ ------------- +
| 1, 17629 |
+ ------------- +
1 row in set (0.02 sec)
Mysql> select count (name) from cve where name not in ('cve-2017-0001 ', 'cve-2017-0002', NULL );
+ ------------- +
| Count (name) |
+ ------------- +
| 0 |
+ ------------- +
1 row in set (0.01 sec)
When the subquery returns NULL, the result must be 0. Check the manual. Therefore, the following query is actually used:
Select count (DISTINCT name)
FROM CVE
WHERE name not in (SELECT cveID FROM cve_sig WHERE cveID is not null)
By the way, it is easy to use regular expression matching in MySQL:
Select count (alarmID)
FROM Alarm
WHERE (cve not rlike '^ CVE-[0-9] {4}-[0-9] {4} $' or cve is null)
Of course, RLIKE can also write REGEXP. I personally prefer to use RLIKE, because the spelling is close to LIKE and can be seen in terms of name and meaning.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service