Today, when I was looking at the laboratory projects, I encountered a "tricky" problem for me. In fact, it was because I was too stupid. Throw out the sql statement first // This statement cannot get the correct query result when id is not 1. select min(id+1) from oslist c where not exists (select id from oslist where id = c.id+1); When I first saw this query statement, I was completely confused, probably because I hadn’t touched SQL for a long time. 1 exists syntax In sql syntax, exists is used to filter results. During the actual execution process, the EXISTS statement loops the outer table and queries the inner table each time the loop is repeated. Substitute the records of the outer table into the subquery one by one. If the subquery result set is empty, it means it does not exist; otherwise, it exists. It should be noted here that substituting the records of the outer table into the subquery is only to see whether the query result is empty, rather than making a substantive value comparison. Let’s take an example: If the id in the table oslist is 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 14, 15. The SQL statement at the beginning of the article is used here, using self-connection. Then the query process is as follows:
End the query. ps: Delete the data with the smallest id in MySQL //Method 1 delete from tablenamewhere id in (select id from (select min(id) id from tablenamec1) t1); //Method 2 delete from table name order by id asc limit 1; Summarize The above is the method that I introduced to you. I hope it will be helpful to you. If you have any questions, please leave me a message and I will reply to you in time. I would also like to thank everyone for their support of the 123WORDPRESS.COM website! You may also be interested in:
|
<<: JavaScript uses promise to handle multiple repeated requests
>>: How to check the hard disk size and mount the hard disk in Linux
CSS Selectors Setting style on the html tag can s...
Table of contents Setting up a basic HTTP request...
Business scenario: querying tables in different d...
virtualenv is a tool for creating isolated Python...
When we design a page, we often need to center th...
Table of contents 1. Overview 1. Explain statemen...
Sometimes you need to install certain dependencie...
Table of contents MySQL delete syntax alias probl...
This article example shares the specific code of ...
Preface Before leaving get off work, the author r...
1. Reason I just needed to reinstall MySQL on a n...
The worst option is to sort the results by time a...
Table of contents 1. Affairs: Four major characte...
AWS - Amazon's cloud computing service platfo...
Download address: https://dev.mysql.com/downloads...