How to realize the function of finding whether it exists in SQL
This article mainly shows you "SQL how to find whether there is a function", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "SQL how to find whether there is a function" this article.
SQL to find out whether "exists". Stop count. It takes time.
There are only two states for querying "yes" and "no" from the database table according to a certain condition, so why do you have to SELECT count (*) when writing SQL?
Whether it is a new programmer who has just entered the profession, or a programmer who has been superb battlefield for many years, it is the same count.
The way most people write it at present
When the REVIEW code is repeated, it is found that the business code needs to query whether there are records according to one or more conditions, regardless of how many records there are. The common SQL and code writing methods are as follows
# SQL: SELECT count (*) FROM table WHERE a = 1 AND b = 2 # Java: int nums = xxDao.countXxxxByXxx (params); if (nums > 0) {/ / when it exists, execute the code here} else {/ / execute the code here} if it does not exist
Does it feel very OK? there is no problem.
Optimization scheme
It is recommended to write as follows:
# SQL: SELECT 1 FROM table WHERE a = 1 AND b = 2 LIMIT Java: Integer exist = xxDao.existXxxxByXxx (params); if (exist! = NULL) {/ / when it exists, execute the code here} else {/ / execute the code here when it does not exist
SQL no longer uses count, but uses LIMIT 1 instead, so that when you encounter an entry in a database query, you can return it instead of looking up how many items there are left. You can directly determine whether it is empty in the business code.
The above is all the content of the article "how to find out if there is a function in SQL". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!