Summary of SQL statement usage

1. SQL statements are the most commonly used by our developers, and they are the most favorite. When I use it, I always feel that I have a lot of places to learn, and I don't fully understand the mystery of SQL statements. In development, slowly accumulating is the best way.

2. The use of grouping statements, do not want to count the number of records containing repeated fields. Then add a distinct modifier to the parameter of the count() function.

E.g:

        (1). Count all records, including duplicate mobile phone numbers.

         

SELECT COUNT(phone_number) FROM app_user;

       The result is 1363

       (2). Statistics of all mobile phone numbers, including unique mobile phone numbers.

SELECT COUNT(DISTINCT  phone_number) FROM app_user;

       The result is 1361

       (3). Count the number of mobile phone numbers greater than 1

SELECT COUNT(phone_number),phone_number FROM app_user GROUP BY phone_number HAVING COUNT( phone_number)>1;

       turn out

       COUNT(phone_number)phone_number

                    3                        15321155407

     Summary: Knowledge points involved: SQL usage, grouping, grouping condition filtering, deduplication, etc. There are many uses of SQL statements, and they are also very subtle. Only a mindful practitioner can comprehend the mystery.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327030843&siteId=291194637