🔴 연산자
1. 연산자와 피연산자의 관계
연산자 : 연산을 수행하는 기호 혹은 키워드
피연산자 : 연산에 참여하는 변수나 값
2. 산술 연산자
SQL 구문에서 바로 산술이 가능하다
연산자 | 의미 | 우선순위 | 표현식 | 예시 |
+ | 더하기 | 2 | 피연산자1 + 피연산자2 | 1 + 4 |
- | 빼기 | 2 | 피연산자1 - 피연산자2 | 5 - 1 |
* | 곱하기 | 1 | 피연산자1 * 피연산자2 | 4 * 3 |
/ | 나누기 | 1 | 피연산자1 / 피연산자2 | 9 / 3 |
🔖 예시
// OrderDetails 테이블에서 ProductID 가 11인 값을 찾고자할때
// 다음 구문 사용시 모두 ProductID 가 11인 값이 조회된다.
SELECT * FROM OrderDetails where ProductID = 10+1;
SELECT * FROM OrderDetails where ProductID = 12-1;
SELECT * FROM OrderDetails where ProductID = 1*11;
SELECT * FROM OrderDetails where ProductID = 22/2;
https://www.w3schools.com/sql/trysql.asp?filename=trysql_select_all
SQL Tryit Editor v1.6
WebSQL stores a Database locally, on the user's computer. Each user gets their own Database object. WebSQL is supported in Chrome, Safari, Opera, and Edge(79). If you use another browser you will still be able to use our Try SQL Editor, but a different ver
www.w3schools.com
3. 비교 연산자
SQL 구문에서 바로 비교연산 가능하다
연산자 | 의미 | 표현식 |
> | 크다 | 피연산자1 > 피연산자2 |
>= | 크거나 같다 | 피연산자1 >= 피연산자2 |
< | 작다 | 피연산자1 < 피연산자2 |
<= | 작거나 같다 | 피연산자1 <= 피연산자2 |
= | 같다 | 피연산자1 = 피연산자2 |
<>, != | 같지 않다 | 피연산자1 != 피연산자2 피연산자1 <> 피연산자2 |
예시
// Product 테이블에서 가격에 범위를 지정해서 원하는 결과를 얻고 싶을때
//가격이 10초과인 값들
SELECT * FROM Products where Price>10;
//가격이 10 이상인값들
SELECT * FROM Products where Price>=10;
//가격이 10 미만인 값들
SELECT * FROM Products where Price<10;
//가격이 10이하인값들
SELECT * FROM Products where Price<=10;
//가격이 10인 값들
SELECT * FROM Products where Price=10;
//가격이 10이 아닌값들
SELECT * FROM Products where Price!=10;
SELECT * FROM Products where Price<>10;
https://www.w3schools.com/sql/trysql.asp?filename=trysql_select_all
SQL Tryit Editor v1.6
WebSQL stores a Database locally, on the user's computer. Each user gets their own Database object. WebSQL is supported in Chrome, Safari, Opera, and Edge(79). If you use another browser you will still be able to use our Try SQL Editor, but a different ver
www.w3schools.com
4. 논리 연산자
SQL 구문에 바로 논리연산 가능하다
연산자 | 의미 | 우선순위 | 표현식 |
NOT | 부정 | 1 | NOT 피연산자 |
AND | 그리고(논리곱) | 2 | 피연산자1 AND 피연산자2 |
OR | 또는(논리합) | 3 | 피연산자1 OR 피연한자2 |
예시
// Customer 테이블에서 Country의 값이 Germany가 아닌 값들
SELECT * FROM Customers where not Country='Germany';
// Customer 테이블에서 Country의 값이 UK 이면서 City 값이 London 인 값들
SELECT * FROM Customers where Country='UK' and City='London';
// Customer 테이블에서 Country의 값이 UK 이거나 City 값이 London 인 값들
SELECT * FROM Customers where Country='UK' or City='London';
https://www.w3schools.com/sql/trysql.asp?filename=trysql_select_all
SQL Tryit Editor v1.6
WebSQL stores a Database locally, on the user's computer. Each user gets their own Database object. WebSQL is supported in Chrome, Safari, Opera, and Edge(79). If you use another browser you will still be able to use our Try SQL Editor, but a different ver
www.w3schools.com
4. 비트 논리 연산자
2진 연산으로 일반연산 보다 속도가 빠르다
주로 BLIND SQLI 에서 많이 사용된다
DBMS | AND | OR | XOR |
ORACLE | bitand(피연산자1,피연산자2) | - | - |
MSSQL | 피연산자1 & 피연산자2 | 피연산자1 | 피연산자2 | 피연산자1 ^ 피연산자2 |
MYSQL | 피연산자1 & 피연산자2 | 피연산자1 | 피연산자2 | 피연산자1 ^ 피연산자2 |
예시
SELECT 1&1;
= 1
SELECT 2&1;
=0
SELECT 3&1;
=1
SELECT 5&3;
=1
5. 연결 연산자
문자열을 연결하는 연산자
DBMS | 연산자 | 표현식 | 예시 |
ORACLE | || | 피연산자1 || 피연산자2 | 'TE' || 'ST' |
MSSQL | + | 피연산자1 + 피연산자2 | 'TE' + 'ST' |
MYSQL | 공백 | 피연산자1 공백 피연산자2 | 'TE' 공백 'ST' |
예시
SELECT * FROM Customers where Country='UK' or City='Lo' 'ndon';
SELECT * FROM Customers where Country='U'K' or City='Lo' 'ndon';
SELECT * FROM Customers where City='Man' 'heim';
// 공통적으로 concat()함수 사용이 가능하다
SELECT * FROM Customers where City=concat('Man','heim');
https://www.w3schools.com/sql/trysql.asp?filename=trysql_select_all
SQL Tryit Editor v1.6
WebSQL stores a Database locally, on the user's computer. Each user gets their own Database object. WebSQL is supported in Chrome, Safari, Opera, and Edge(79). If you use another browser you will still be able to use our Try SQL Editor, but a different ver
www.w3schools.com
6. 연결 연산자
IN 연산자 사용( OR 연산자를 연속적으로 사용하는것과 똑같다
SELECT 컬럼명1, 컬럼명2 FROM 테이블명 WHERE 컬럼값 IN(레코드값,레코드값);
예시
// CUstomers 테이블에서 City의 값이 london 과 madrid인 값만 보여줌
SELECT * FROM Customers where City in ('London', 'Madrid');
NOT IN 연산자 사용
SELECT 컬럼명1, 컬럼명2 FROM MEMBER WHERE 컬럼값 NOT IN(레코드값, 레코드값);
예시
// CUstomers 테이블의 Cityrkqtdl London과 Madrid를 제외한 값을 모두 보여줌
SELECT * FROM Customers where City NOT In ('London', 'Madrid');
https://www.w3schools.com/sql/trysql.asp?filename=trysql_select_all
SQL Tryit Editor v1.6
WebSQL stores a Database locally, on the user's computer. Each user gets their own Database object. WebSQL is supported in Chrome, Safari, Opera, and Edge(79). If you use another browser you will still be able to use our Try SQL Editor, but a different ver
www.w3schools.com
7. LIKE 연산자
문자 패턴에 의해 검색할수 있는 연산자
와일드카드 | 의미 | 예시 |
% | 모든문자 | id like 'ad%' |
_ | 하나의 문자 | id like 'ad_in' |
예시
// Cusomers 테이블의 city 값이 London인것을 찾기
SELECT * FROM Customers where City like '%Lon%';
SELECT * FROM Customers where City like 'Londo_';
https://www.w3schools.com/sql/trysql.asp?filename=trysql_select_all
SQL Tryit Editor v1.6
WebSQL stores a Database locally, on the user's computer. Each user gets their own Database object. WebSQL is supported in Chrome, Safari, Opera, and Edge(79). If you use another browser you will still be able to use our Try SQL Editor, but a different ver
www.w3schools.com
'Programming > Database' 카테고리의 다른 글
[Database] SQL ORDER BY 절을 이용한 정렬 (0) | 2022.09.23 |
---|---|
[Database] SQL 조건문 (0) | 2022.09.23 |
[Database] SQL 함수 (0) | 2022.09.23 |
[Database] SQL 기본문법(INSERT, SELECT, UPDATE, DELETE) (0) | 2022.09.23 |
[Database]SQL 기본문법 (CREATE, DROP, ALTER) (1) | 2022.09.23 |