Mysql function uses real

A real purpose

Operation data using various functions, and usage of various control functions.

Two combat operation

1 using the mathematical function RAND () to generate a random integer less than 10 3.

RAND () function generates a random number between 0 and 1, to generate a random number between 0 ~ 10, RAND () is multiplied by 10, if required to be integers, then removing the decimal portion must also result in as used herein, the ROUND () function, the following process is performed:

mysql> SELECT ROUND(RAND() * 10),  ROUND(RAND() * 10), ROUND(RAND() * 10);
+--------------------+--------------------+--------------------+
| ROUND(RAND() * 10) | ROUND(RAND() * 10) | ROUND(RAND() * 10) |
+--------------------+--------------------+--------------------+
|                  6 |                  4 |                 10 |
+--------------------+--------------------+--------------------+
1 row in set (0.01 sec)

2 using SIN (), COS (), TAN (), COT () function is a trigonometric function values, and converts the results to integer values.

MySQL, trigonometric function value calculated from the integral value is not necessarily need to use a mathematical function to convert it to an integer, which may be used with a mathematical function ROUND (), FLOOR (), etc., performs as follows:

mysql> SELECT PI(), sin(PI()/2),cos(PI()), ROUND(tan(PI()/4)), FLOOR(cot(PI()/4));
+----------+-------------+-----------+--------------------+--------------------+
| PI()     | sin(PI()/2) | cos(PI()) | ROUND(tan(PI()/4)) | FLOOR(cot(PI()/4)) |
+----------+-------------+-----------+--------------------+--------------------+
| 3.141593 |           1 |        -1 |                  1 |                  1 |
+----------+-------------+-----------+--------------------+--------------------+
1 row in set (0.01 sec)

3 create a table, and string and date functions, operation field value.

(1) Member create a table, which contains three fields, namely a field m_id AUTO_INCREMENT constraints, m_FN field type VARCHAR, VARCHAR m_LN type field, and the field m_birth DATETIME type VARCHAR m_info type field.

(2) insert a record, m_id default value, m_FN value "Halen", m_LN value "Park", m_birth value 1970-06-29, m_info value "GoodMan".

(3) returns the length m_FN returns the full name, the field value m_info first record into human lowercase. The reverse output value m_info.

(4) Age is the first record of the human, and calculates the location value in the year m_birth field, in accordance with the "Saturday October 4th 1997" format of the output time value.

(5) inserting a new record, m_FN value "Samuel", m_LN value of "Green", m_birth current system time value, m_info empty. Using the LAST_INSERT_ID () Check the last inserted ID value.

Proceed as follows:

(1) create a table member, enter the following statement:

CREATE TABLE member
     (
     m_id    INT AUTO_INCREMENT PRIMARY KEY,
     m_FN   VARCHAR(100),
     m_LN   VARCHAR(100),
     m_birth  DATETIME,
     m_info   VARCHAR(255) NULL
     );
Query OK, 0 rows affected (0.01 sec)

(2) insert a record, input sentence as follows:

mysql> INSERT INTO member VALUES (NULL, 'Halen ', 'Park', '1970-06-29', 'GoodMan ');
Query OK, 1 row affected (0.04 sec)

Use a SELECT statement to see the result of the insertion,

mysql> SELECT * FROM member;
+------+--------+------+---------------------+----------+
| m_id | m_FN   | m_LN | m_birth             | m_info   |
+------+--------+------+---------------------+----------+
|    1 | Halen  | Park | 1970-06-29 00:00:00 | GoodMan  |
+------+--------+------+---------------------+----------+
1 row in set (0.00 sec)

(3) returns the length m_FN returns the full name of the human article recording, converting to lower case m_info field value, the value of the inverted output m_info.

mysql> SELECT LENGTH(m_FN), CONCAT(m_FN, m_LN),LOWER(m_info), REVERSE(m_info) FROM member;
+--------------+--------------------+---------------+-----------------+
| LENGTH(m_FN) | CONCAT(m_FN, m_LN) | LOWER(m_info) | REVERSE(m_info) |
+--------------+--------------------+---------------+-----------------+
|            6 | Halen Park         | goodman       |  naMdooG        |
+--------------+--------------------+---------------+-----------------+
1 row in set (0.00 sec)

(4) Age is the first record of the human, and calculates the location value in the year m_birth field, in accordance with the "Saturday October 4th 1997" format of the output time value.

SELECT YEAR(CURDATE())-YEAR(m_birth) AS age, DAYOFYEAR(m_birth) AS days, DATE_FORMAT(m_birth, '%W %D %M %Y') AS birthDate FROM member;

Statement execution results are as follows:

mysql> SELECT YEAR(CURDATE())-YEAR(m_birth) AS age, DAYOFYEAR(m_birth) AS days, DATE_FORMAT(m_birth, '%W %D %M %Y') AS birthDate FROM member;
+------+------+-----------------------+
| age  | days | birthDate             |
+------+------+-----------------------+
|   50 |  180 | Monday 29th June 1970 |
+------+------+-----------------------+
1 row in set (0.01 sec)

(5) inserting a new record, m_FN value "Samuel", m_LN value of "Green", m_birth current system time value, m_info empty. Using the LAST_INSERT_ID () Check the last inserted ID value.

mysql> INSERT INTO member VALUES (NULL, 'Samuel', 'Green', NOW(),NULL);
Query OK, 1 row affected (0.03 sec)

View the results using the SELECT statement inserted:

mysql> SELECT * FROM member;
+------+--------+-------+---------------------+----------+
| m_id | m_FN   | m_LN  | m_birth             | m_info   |
+------+--------+-------+---------------------+----------+
|    1 | Halen  | Park  | 1970-06-29 00:00:00 | GoodMan  |
|    2 | Samuel | Green | 2020-01-12 18:32:25 | NULL     |
+------+--------+-------+---------------------+----------+
2 rows in set (0.00 sec)

Can be seen, there are two records in the table, then use the LAST_INSERT_ID () function looks at the value of the last inserted ID, input sentence as follows:

mysql> SELECT LAST_INSERT_ID();
+------------------+
| LAST_INSERT_ID() |
+------------------+
|                2 |
+------------------+
1 row in set (0.00 sec)

Finally, the second record is inserted, its ID value of 2, so the return value is 2.

CASE using conditional judgment, if m_birth less than 2000, display "old", if m_birth greater than 2000, then the display "young", the input sentence as follows:

SELECT m_birth, CASE WHEN YEAR(m_birth) < 2000  THEN  'old'
     WHEN YEAR(m_birth) > 2000 THEN  'young'
     ELSE 'not born' END AS status FROM member;
+---------------------+--------+
| m_birth             | status |
+---------------------+--------+
| 1970-06-29 00:00:00 | old    |
| 2020-01-12 18:32:25 | young  |
+---------------------+--------+
2 rows in set (0.00 sec)

 

 

Released 4080 original articles · won praise 538 · Views 2.96 million +

Guess you like

Origin blog.csdn.net/chengqiuming/article/details/103948219