SQL 다이어그램 설명 작업 16 - 단순 함수 처리
8610 단어 MySQLSQL 문MySQL 기본 작업
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)
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Redash를 사용할 때 몰랐던 SQL을 쓰는 법을 배웠습니다.최근 redash에서 sql을 쓸 기회가 많고, 이런 쓰는 방법이 있었는지와 sql에 대해 공부를 다시하고 있기 때문에 배운 것을 여기에 씁니다. Redash란? 월별로 데이터를 표시하고 싶습니다 주별로 데이터를 표...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.