From: http://www.php100.com/html/webkaifa/database/Mysql/2011/0710/8418.html
In the past, this site introduced "20 + best MySQL performance optimization experiences". Today, we will introduce six useful MySQL SQL statements, many may use PHP to implement these functions.
1. Calculate the number of years
You figured out how old this person is on your birthday.
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 their format is 'yyyy-mm-dd hh: mm: ss'. The difference between them is:
Unix_timestamp (dt2)-unix_timestamp (dt1) |
Dividing by 60 is the number of minutes in the difference, dividing by 3600 is the number of hours in the difference, and dividing by 24 is the number of days in the difference.
3. display the value of a column that appears n times
Select ID From TBL Group by ID Having count (*) = N; |
4. Calculate the workdays between two days
Workdays are divided into Saturday and Sunday holidays.
Select count (*) From calendar Where d between start and stop And dayofweek (d) not in (1, 7) And holiday = 0; |
5. Search for the primary key in the table
Select K. column_name From information_schema.table_constraints t Join information_schema.key_column_usage K Using (constraint_name, table_schema, table_name) Where T. constraint_type = 'Primary key' And T. table_schema = 'db' And T. table_name = TBL' |
6. Check the size of your database.
select table_schema as 'db name', round (sum (data_length + index_length)/1024/1024, 3) as 'db size (MB) ', round (sum (data_free)/1024/1024, 3) as 'free space (MB) ' from information_schema.tables group by table_schema; |