What are the benefits of using the case method in mysql
This article mainly introduces the benefits of using the case method in mysql, which has certain reference value, and friends who need it can refer to it. I hope you all have a lot of success after reading this article. Let's take a look at it together.
The use of mysql case method benefits: 1, in the display of query results can be flexible organization format;2, effectively avoid multiple visits to the same table or several tables.
Benefits of using MySQL's case method:
The biggest advantage of using select and case in combination is that it can be organized flexibly when displaying query results, and the other is to effectively avoid multiple visits to the same table or several tables.
Here's a simple example. For example, the table students(id, name ,birthday, sex, grade) requires statistics on the number of boys and girls in each grade.
The heading of the statistical results is grade, number of boys, number of girls. If you don't use select case when, in order to display the number of men and women side by side, it is very troublesome to count, first determine the grade information, then take the number of boys and girls according to the grade, and it is easy to make mistakes.
Select case when written as follows:
SELECT grade, COUNT (CASE WHEN sex = 1 THEN 1 ELSE NULL END) Number of boys, COUNT (CASE WHEN sex = 2 THEN 1 ELSE NULL END) Number of girls FROM studentsGROUP BY grade; Thank you for reading this article carefully. I hope Xiaobian can share the benefits of using the case method in mysql. What content is helpful to everyone? At the same time, I hope everyone will support more. Pay attention to the industry information channel. When you encounter problems, find detailed solutions waiting for you to learn!