MySQL 계산하거나 문자열을 결합해서 표시하기, MySQL 함수 사용하기
MySQL 컬럼 값을 계산해서 표시하기
컬럼이 a, b가 있다면 a, b끼리 사칙 연산을 해서 출력할 수 있습니다.
더하기 : select a + b from 테이블명;
빼기: select a – b from 테이블명;
곱하기: select a * b from 테이블명;
나누기: select a / b from 테이블명;
table_sales 테이블에 있는 sales 컬럼에 1000배로 하고 별명을 매출로 출력해 보겠습니다.
select sales * 1000 as 매출 from table_sales;
![](https://t1.daumcdn.net/cfile/tistory/99F3B0335A0540011A)
MySQL 함수사용하기.
평균 구하기.
avg( ) 는 평균을 구하는 함수 입니다.
table_sales 의 sales 평균을 구해 보겠습니다.
select avg(sales) from table_sales;
![](https://t1.daumcdn.net/cfile/tistory/9943A8335A05402611)
합계 구하기.
sum( ) 은 합계를 구하는 함수 입니다.
table_sales 의 sales 합계를 구해 보겠습니다.
select sum(sales) from table_sales;
![](https://t1.daumcdn.net/cfile/tistory/99A62C335A05404D11)
데이터 개수 표시하기.
count() 함수는 데이터 개수를 계산하는 함수 입니다.
table_sales 의 sales 개수를 구해 보겠습니다.
select count(sales) from table_sales;
![](https://t1.daumcdn.net/cfile/tistory/99BFCD335A05406D23)
각종 정보를 표시하는 함수.
원주율을 계산하는 함수 pi( );
select pi( );
![](https://t1.daumcdn.net/cfile/tistory/9970A5335A05408D0F)
MySQL 서버 버전 표시하기: version();
select version( );
![](https://t1.daumcdn.net/cfile/tistory/997226335A0540AE13)
MySQL 현재 사용중인 데이터베이스 표시하기: database( );
select database( );
![](https://t1.daumcdn.net/cfile/tistory/99FF61335A0540CC20)
MySQL 현재 사용자 표시하기: user( );
select user( );
![](https://t1.daumcdn.net/cfile/tistory/998BB1335A05411E2C)
MySQL 문자열 결합하기: concat( );
테이블 table_01 에 number + name + ‘님’을 결합해서 표시해 보겠습니다.
select concat(number, name, ‘님’) from table_01;
![](https://t1.daumcdn.net/cfile/tistory/99303C335A05417F3C)
MySQL 오른쪽 문자부터 추출하기 : right( );
select right(number, 1) from table_01;
![](https://t1.daumcdn.net/cfile/tistory/991862335A05419F2B)
MySQL 왼쪽 문자부터 추출하기: left( );
select left(name, 1) from table_01;
![](https://t1.daumcdn.net/cfile/tistory/9999A9335A0541BC20)
MySQL 위치 지정하여 추출하기: substring( );
name 컬럼 2번째 문자부터 2개의 문자만 표시하기.
select substring(name, 2, 2) from table_01;
![](https://t1.daumcdn.net/cfile/tistory/997602335A0541D804)
MySQL 반복해서 표시하기: repeat( );
‘-‘ 문자를 Age 컬럼에 있는 숫자만큼 반복해서 표시합니다.
select repeat(‘-‘, age) from table_01;
![](https://t1.daumcdn.net/cfile/tistory/9922BE335A0541F424)
MySQL 문자열을 거꾸로 표시하기: reverse( );
select reverse(name) from table_01;
![](https://t1.daumcdn.net/cfile/tistory/999CCA335A05421A07)
MySQL 날짜와 시간을 표시하는 함수: now( );
now( ) 함수는 현재 날짜와 시간을 반환하는 함수입니다. 데이터를 처리한 날짜와 시간을 자동으로 입력하려면, 컬럼 이름 대신 now( ) 함수를 입력합니다.
오늘 날짜와 시간을 출력해 보겠습니다.
select now();
![](https://t1.daumcdn.net/cfile/tistory/998C1A335A05423721)