Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the function of finding whether it exists in SQL

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report