본문 바로가기
정상을향해/Data·Machine Learning

MySQL 명령어 (2)

by 사이테일 2013. 11. 10.

 

이번에는 완성된 데이터베이스의 정보에 접근하는 과정에 대해서 알아보도록 하겠습니다.

아래 SQL 파일로 실습에 필요한 데이터베이스를 만들어놓기 바랍니다.

 


Prac2.sql


 

MySQL 5.5.23 버전과

SQLGate2010 SQL 쿼리 편집기를 사용했습니다.

 

 

위의 파일로 DB를 구축하면 다음과 같은 테이블 3개가 만들어 집니다.

SELECT * FROM 테이블명

을 이용해 완성된 테이블을 확인하면 되겠죠?

 

 

 

 

ordweight > 5 customer custname을 찾아보자.

SELECT custname FROM customer, ordinfo WHERE customer.custno = ordinfo.custno AND ordinfo.ordweight > 5;

--

 

SELECT custname FROM customer WHERE customer.custno IN (SELECT custno FROM ordinfo WHERE ordinfo.ordweight > 5);

--

 

 

 

여기서 쓰인 IN 연산자는 괄호 안의 조건 중 어느 하나만 만족하더라도 WHERE절이 참이 되도록 합니다.

 

예를 들면 다음과 같이 사용하면,

SELECT * FROM ordinfo WHERE ordweight IN (3, 5, 16);

ordinfo 테이블에서 ordweight 3, 5, 16 중 어느 하나만 만족하더라도 출력하게 됩니다.

 

 

 

 

ordweight 18 또는 21 customer custname을 찾아보자.

SELECT custname FROM customer, ordinfo WHERE customer.custno = ordinfo.custno AND ordinfo.ordweight = 21 UNION

SELECT custname FROM customer, ordinfo WHERE customer.custno = ordinfo.custno AND ordinfo.ordweight = 18;

-- UNION 2개 이상의 테이블을 묶어주는 역할을 합니다.

-- ,  SELECT의 결과 값들이 겹쳐진 결과라고 생각하시면 됩니다.

 

 

 

 

ordweight > 5 이지만 Paris에 살지 않는 customer custname을 찾아보자.

SELECT custname FROM customer, ordinfo WHERE customer.custno = ordinfo.custno AND ordinfo.ordweight > 5 AND customer.custcity NOT IN (SELECT custcity FROM customer WHERE custcity = 'Paris');

--

 

SELECT custname FROM customer WHERE customer.custno IN (SELECT custno FROM ordinfo WHERE ordinfo.ordweight > 5) AND customer.custcity NOT IN (SELECT custcity FROM customer WHERE custcity = 'Paris');

--

 

 

 

 

custbal > custbal of Josh customer custname을 찾아보자.

SELECT custname FROM customer WHERE customer.custbal > ANY (SELECT custbal FROM customer WHERE customer.custname = 'Josh');

--

 

 

 

 

The highest custbal을 가진 customer custname을 찾아보자.

SELECT custname FROM customer WHERE customer.custbal >= ALL (SELECT custbal FROM customer);

--

 

 

 

 

custbal < 1000 customers custbal 평균을 구해보자.

SELECT AVG (custbal) FROM customer WHERE customer.custbal < 1000;

--

 

 

 

 

'정상을향해 > Data·Machine Learning' 카테고리의 다른 글

MySQL 명령어 (3)  (0) 2013.11.10
MySQL 명령어 (1)  (0) 2013.11.10