Several small tips statements in MySQL

Source: Internet
Author: User
Summary of small tips commonly used in six MySQL statements

1) For example, to calculate the current size of a person, assume that the variable of the person's birthdate is @ dateofbirth,

Then

Select date_format (from_days (to_days (now ()-to_days (@ dateofbirth), '% y') + 0;

2 Calculate the date difference

Assume that the two dates are in the format of yyyy-mm-dd hh: mm: SS, you can calculate the number of seconds of the difference.

Unix_timestamp (dt2)-unix_timestamp (dt1)

Then remove 60, 3600, * 24 to get the minute, hour, and day.

3. Calculate the number of times a column value appears.

Select ID from TBL group by ID having count (*) = N;

4. Search for the primary key of a 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'

5 view the size of a 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;

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.