SQL 다이어그램 설명 작업 16 - 단순 함수 처리

| 저작권 주장: 본 논문은 블로거가 창작한 글로 블로거의 허락 없이 전재할 수 없습니다.전재는 원래 링크, 블로그 주소:https://blog.csdn.net/sgsgy5
1.7 함수
1.7.1 디지털 클래스
mysql> select rand();			#      
+---------------------+
| rand()              |
+---------------------+
| 0.18474003969201822 |
+---------------------+
1 row in set (0.00 sec)

mysql> select * from stuinfo order by rand();   #     

mysql> select * from stuinfo order by rand() limit 2;    #        
+--------+----------+--------+--------+---------+------------+
| stuNo  | stuName  | stuSex | stuAge | stuSeat | stuAddress |
+--------+----------+--------+--------+---------+------------+
| s25305 |              |        |     23 |       7 |              |
| s25304 |             |         |     28 |       4 |              |
+--------+----------+--------+--------+---------+------------+
2 rows in set (0.00 sec)

mysql> select round(3.5);     #    
+------------+
| round(3.5) |
+------------+
|          4 |
+------------+
1 row in set (0.00 sec)

mysql> select ceil(3.1);	#     
+-----------+
| ceil(3.1) |
+-----------+
|         4 |
+-----------+
1 row in set (0.00 sec)

mysql> select floor(3.9);	#     
+------------+
| floor(3.9) |
+------------+
|          3 |
+------------+
1 row in set (0.00 sec)

mysql> select truncate(3.1415926,3);	#     
+-----------------------+
| truncate(3.1415926,3) |
+-----------------------+
|                 3.141 |
+-----------------------+
1 row in set (0.00 sec)

1.7.2 문자열 클래스
mysql> select ucase('i am a boy!');		#     
+----------------------+
| ucase('i am a boy!') |
+----------------------+
| I AM A BOY!          |
+----------------------+
1 row in set (0.00 sec)

mysql> select lcase('I Am A Boy!');		#    
+----------------------+
| lcase('I Am A Boy!') |
+----------------------+
| i am a boy!          |
+----------------------+
1 row in set (0.00 sec)

mysql> select left('abcde',3);		#        ,  3 
+-----------------+
| left('abcde',3) |
+-----------------+
| abc             |
+-----------------+
1 row in set (0.00 sec)

mysql> select right('abcde',3);		#        ,  3 
+------------------+
| right('abcde',3) |
+------------------+
| cde              |
+------------------+
1 row in set (0.00 sec)

mysql> select substring('abcde',2,3);	#  2       ,  3 【   1  】
+------------------------+
| substring('abcde',2,3) |
+------------------------+
| bcd                    |
+------------------------+
1 row in set (0.00 sec)

mysql> select concat('  ','  ');	#      
+-----------------------+
| concat('  ','  ')       |
+-----------------------+
|                         |
+-----------------------+
1 row in set (0.00 sec)

mysql> select concat(stuname,'-',stusex) from stuinfo;  #              
+----------------------------+
| concat(stuname,'-',stusex) |
+----------------------------+
|    -                           |
|    -                          |
|    -                         |
|     -                          |
|     -                          |
|     -                          |
|    -                         |
+----------------------------+
7 rows in set (0.00 sec)

# coalesce(  1,  2)      1        1,  ,    2
mysql> select stuname,coalesce(writtenexam,'  '),coalesce(labexam,'  ') from stuinfo natural left join stumarks;   #              
+----------+------------------------------+--------------------------+
| stuname  | coalesce(writtenexam,'  ')    | coalesce(labexam,'  ')    |
+----------+------------------------------+--------------------------+
|             | 77                           | 82                       |
|            | 50                           | 90                       |
|            | 88                           | 58                       |
|             | 65                           | 50                       |
|              |                               |                           |
|             | 56                           | 48                       |
|            |                               |                           |
+----------+------------------------------+--------------------------+

mysql> select length('     ');		#     
+----------------------+
| length('     ')          |
+----------------------+
|                   10 |
+----------------------+
1 row in set (0.00 sec)

mysql> select char_length('     ');		#     
+---------------------------+
| char_length('     ')          |
+---------------------------+
|                         5 |
+---------------------------+
1 row in set (0.00 sec)

1.7.3 시간대
mysql> select unix_timestamp();	#     
+------------------+
| unix_timestamp() |
+------------------+
|       1537084508 |
+------------------+
1 row in set (0.00 sec)

mysql> select from_unixtime(unix_timestamp());	#        - -    :  :    
+---------------------------------+
| from_unixtime(unix_timestamp()) |
+---------------------------------+
| 2018-09-16 15:55:56             |
+---------------------------------+
1 row in set (0.00 sec)

mysql> select now();		#         
+---------------------+
| now()               |
+---------------------+
| 2018-09-16 15:57:04 |
+---------------------+
1 row in set (0.00 sec)

mysql> select year(now())  ,month(now())  , day(now())  ,hour(now())  ,minute(now())   ,second(now())  ;
+------+------+------+------+------+------+
|       |       |       |      |        |       |
+------+------+------+------+------+------+
| 2018 |    9 |   16 |   15 |   59 |   14 |
+------+------+------+------+------+------+
1 row in set (0.00 sec)

mysql> select dayname(now())   ,monthname(now()),dayofyear(now())       ;
+--------+------------------+--------------+
|          | monthname(now()) |                  |
+--------+------------------+--------------+
| Sunday | September        |          259 |
+--------+------------------+--------------+
1 row in set (0.00 sec)

mysql> select datediff(now(),'2008-8-8');	#     
+----------------------------+
| datediff(now(),'2008-8-8') |
+----------------------------+
|                       3691 |
+----------------------------+
1 row in set (0.00 sec)

mysql> select convert(now(),date),convert(now(),time);	#  now()       
+---------------------+---------------------+
| convert(now(),date) | convert(now(),time) |
+---------------------+---------------------+
| 2018-09-16          | 16:07:24            |
+---------------------+---------------------+

mysql> select cast(now() as date),cast(now() as time);   #  now()       
+---------------------+---------------------+
| cast(now() as date) | cast(now() as time) |
+---------------------+---------------------+
| 2018-09-16          | 16:08:03            |
+---------------------+---------------------+
1 row in set (0.00 sec)

1.7.4 암호화 함수
+----------------------------------+------------------------------------------+
| md5('root')                      | sha('root')                              |
+----------------------------------+------------------------------------------+
| 63a9f0ea7bb98050796b649e85481845 | dc76e9f0c0006e8f919e0c515c66dbba3982f785 |
+----------------------------------+------------------------------------------+
1 row in set (0.00 sec)

1.7.5 판단 함수
구문
if(   , 1, 2)

예제:
mysql> select if(10%2=0,'  ','  ');
+--------------------------+
| if(10%2=0,'  ','  ')        |
+--------------------------+
|                           |
+--------------------------+
1 row in set (0.00 sec)

#         60    
mysql> select stuname,ch,math,if(ch>=60 && math>=60,'  ','   ') '    ' from stu;
+----------+------+------+----------+
| stuname  | ch   | math |            |
+----------+------+------+----------+
|             |   80 | NULL |            |
|            |   77 |   76 |           |
|            |   55 |   82 |            |
|             | NULL |   74 |            |
|              |   72 |   56 |            |
|             |   86 |   92 |           |
|            |   74 |   67 |           |
| Tom      |   65 |   67 |           |
| Tabm     |   88 |   77 |           |
+----------+------+------+----------+
9 rows in set (0.00 sec)

좋은 웹페이지 즐겨찾기