Common mysql statements at work share effects that can be achieved without php
- 2020-05-13 03:35:21
- OfStack
Count the years
You want to figure out how old the person is on his birthday.
2. The difference between two times
Get the difference between the two datetime values. If dt1 and dt2 are of type datetime in the form 'yyyy-mm-dd hh:mm:ss', then the number of seconds between them is:
UNIX_TIMESTAMP(dt2) -UNIX_TIMESTAMP (dt1) divided by 60 is the number of minutes missed, divided by 3600 is the number of hours missed, and divided by 24 is the number of days missed.
3. Display the value of N in a column
4. Calculate the working days between the two days
The working day is Sunday except Saturday and holidays.
5. Look up the primary key in the table
Check how big your database is
You want to figure out how old the person is on his birthday.
SELECT DATE_FORMAT(FROM_DAYS(TO_DAYS(now()) - TO_DAYS(@dateofbirth)) . '%Y') + 0;
2. The difference between two times
Get the difference between the two datetime values. If dt1 and dt2 are of type datetime in the form 'yyyy-mm-dd hh:mm:ss', then the number of seconds between them is:
UNIX_TIMESTAMP(dt2) -UNIX_TIMESTAMP (dt1) divided by 60 is the number of minutes missed, divided by 3600 is the number of hours missed, and divided by 24 is the number of days missed.
3. Display the value of N in a column
SELECT id
FROM tbl
GROUP BY id
HAVING COUNT(*) = N;
4. Calculate the working days between the two days
The working day is Sunday except Saturday and holidays.
SELECT COUNT(*)
FROM calendar
WHERE d BETWEEN Start AND Stop
AND DAYOFWEEK(d) NOT IN(1,7)
AND holiday=0;
5. Look up 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'
Check how big your database is
ELECT
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 ;