Six useful SQL statements for MySQL database operations

Source: Internet
Author: User

InMySQL databaseOperations, we often write someSQL statementTo implement the functions you want. However, it seems difficult for beginners of MySQL databases. This article summarizes six useful SQL statements. Beginners can use the following format. Let's take a look at this part.

1. Calculate the number of years

If you want to calculate the person's age by birthday, you can use the following statement:

 
 
  1. SELECT DATE_FORMAT(FROM_DAYS(TO_DAYS(now()) - TO_DAYS(@dateofbirth)), '%Y') + 0; 

2. Difference between two time points

Returns the difference between two datetime values. Assume that dt1 and dt2 are of the datetime type and are in the format of 'yyyy-mm-dd hh: mm: ss'. The difference between them is in the number of seconds: UNIX_TIMESTAMP (dt2) -UNIX_TIMESTAMP (dt1) divided by 60 is the number of minutes in the difference, divided by 3600 is the number of hours in the difference, and then divided by 24 is the number of days in the difference.

3. display the value of a column that appears N times

 
 
  1. SELECT id  
  2.  
  3. FROM tbl  
  4.  
  5. GROUP BY id  
  6.  
  7. HAVING COUNT(*) = N; 

4. Calculate the workdays between two days

Workdays are divided into Saturday and Sunday holidays.

 
 
  1. SELECT COUNT(*)  
  2.  
  3. FROM calendar  
  4.  
  5. WHERE d BETWEEN Start AND Stop  
  6.  
  7. AND DAYOFWEEK(d) NOT IN(1,7)  
  8.  
  9. AND holiday=0; 

5. Search for the primary key in the table

 
 
  1. SELECT k.column_name  
  2.  
  3. FROM information_schema.table_constraints t  
  4.  
  5. JOIN information_schema.key_column_usage k  
  6.  
  7. USING (constraint_name,table_schema,table_name)  
  8.  
  9. WHERE t.constraint_type='PRIMARY KEY' 
  10.  
  11. AND t.table_schema='db' 
  12.  
  13. AND t.table_name=tbl' 

6. Check the size of your database.

 
 
  1. ELECT  
  2.  
  3. table_schema AS 'Db Name',  
  4.  
  5. Round( Sum( data_length + index_length ) / 1024 / 1024, 3 ) AS 'Db Size (MB)',  
  6.  
  7. Round( Sum( data_free ) / 1024 / 1024, 3 ) AS 'Free Space (MB)'  
  8.  
  9. FROM information_schema.tables  
  10.  
  11. GROUP BY table_schema ; 

This is the introduction of useful SQL statements for MySQL databases. If you want to learn more about MySQL databases, please refer to the article http://database.51cto.com/mysql/, which will surely be useful to you!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.