ν‹°μŠ€ν† λ¦¬ λ·°

#2 μ—μ„œλŠ” μœ μš©ν•œ ν‚€μ›Œλ“œλ“€μ„ μ •λ¦¬ν•˜λ©΄μ„œ λ¬Έμ œλ„ λͺ‡ 개 정리할 μƒκ°μž…λ‹ˆλ‹€.


- μ§‘κ³„ν•¨μˆ˜(SUM, MAX, MIN, COUNT, AVG)

μ§‘κ³„ν•¨μˆ˜λŠ” μ΅œλŒ“κ°’ ν˜Ήμ€ μ΅œμ†Ÿκ°’ κ΅¬ν•˜κΈ°, κ°œμˆ˜ μ„ΈκΈ° κ·Έλ¦¬κ³  ν‰κ·  κ΅¬ν•˜κΈ° λ“±μ— 이용될 수 μžˆμŠ΅λ‹ˆλ‹€.

❗#1μ—μ„œ μ •λ¦¬ν•œ GROUP BYλŠ” μ§‘κ³„ν•¨μˆ˜μ™€ μ‚¬μš©ν•˜λ©΄ μœ μš©ν•˜λ‹ˆ κΌ­ μ•Œμ•„λ‘‘μ‹œλ‹€. 이에 κ΄€λ ¨λœ μ˜ˆμ œλŠ” 뒀에 GROUP BY λ¬Έμ œμ™€ ν•¨κ»˜ μ•Œμ•„λ΄…λ‹ˆλ‹€.


- 쀑볡 제거 ν•˜κΈ°(DISTINCT)

DISINCT ν‚€μ›Œλ“œλ₯Ό μ‚¬μš©ν•΄μ„œ 쀑볡을 μ œκ±°ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

β—μΆ”κ°€λ‘œ μ§‘κ³„ν•¨μˆ˜μ™€ μ‘°ν•©ν•˜μ—¬ COUNT(DISTINCT NAME) 이런 방식도 κ°€λŠ₯ν•©λ‹ˆλ‹€. NAME의 쀑볡값을 미리 μ œκ±°ν•˜κ³  COUNTν•˜λŠ” 것이죠.

-- NAME SELECT 쀑볡 제거
SELECT DISTINCT NAME FROM MEMBER

-- MEMBER ν…Œμ΄λΈ”μ—μ„œ  NULL이 μ•„λ‹Œ NAME의 수λ₯Ό ꡬ할 λ•Œ, μ€‘λ³΅λ˜λŠ” 이름을 ν•˜λ‚˜λ‘œ 치고 COUNT
SELECT COUNT(DISTINCT NAME) 
FROM MEMBER
WHERE NAME IS NOT NULL

 


- μ‹œκ°„ κ΄€λ ¨ ν•¨μˆ˜(DATE_FORMAT, HOUR, DAY, MONTH)

주둜 μ‹œκ°„μ„ λΉ„κ΅ν•˜κ±°λ‚˜ 주어진 format으둜 좜λ ₯ν•˜λŠ” λ¬Έμ œμ—μ„œ ν•„μš”ν•©λ‹ˆλ‹€.

μ‹œκ°„ 비ꡐλ₯Ό μœ„ν•΄μ„œλŠ” HOUR(datetime), DAY(date) λ“± ν•΄λ‹Ήν•˜λŠ” 값을 λ°˜ν™˜ν•˜μ—¬ λΉ„κ΅ν•˜λŠ” 상황에 μ‚¬μš©ν•  수 있고.

좜λ ₯μ—μ„œλŠ” DATE_FORMAT(date, format)을 μ΄μš©ν•΄ 정해진 format으둜 좜λ ₯ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

μ•„λž˜ μ‚¬μ΄νŠΈμ— MySQL ν•¨μˆ˜λ“€μ΄ μ•„μ£Ό 잘 μ •λ¦¬λ˜μ–΄μžˆμ–΄μ„œ κ°€μ Έμ™”μŠ΅λ‹ˆλ‹€.
ν•¨μˆ˜μ— λ§žλŠ” μ˜ˆμ‹œμ™€ νŒŒλΌλ―Έν„°κ°€ μ •λ¦¬λ˜μ–΄μžˆμœΌλ‹ˆ 검색 κΈ°λŠ₯을 ν™œμš©ν•˜λ©΄ μœ μš©ν•  것 κ°™μŠ΅λ‹ˆλ‹€.

www.w3schools.com/sql/sql_ref_mysql.asp

 

MySQL Functions

MySQL Functions MySQL has many built-in functions. This reference contains string, numeric, date, and some advanced functions in MySQL. MySQL String Functions Function Description ASCII Returns the ASCII value for the specific character CHAR_LENGTH Returns

www.w3schools.com


- κ²€μƒ‰μ‘°κ±΄(IN, LIKE, REGEXP)

IN(values)λŠ” values에 ν•΄λ‹Ήν•˜λŠ” κ°’λ§Œ κ°€μ Έμ˜΅λ‹ˆλ‹€. μ—¬κΈ°μ„  μ•„λž˜ μ˜ˆμ‹œλ“€μ„ 보면 λΉ λ₯Έ 이해가 κ°€λŠ₯ν•  κ²λ‹ˆλ‹€.

문제 μΆœμ²˜λŠ” 이미지 μ•„λž˜ 주석에 μžˆμœΌλ‹ˆ μ°Έκ³ ν•΄μ£Όμ„Έμš”.

https://programmers.co.kr/learn/courses/30/lessons/59046

 

LIKE ν‚€μ›Œλ“œλŠ” μœ μ‚¬ν•œ 것듀을 μ°Ύμ•„μ€λ‹ˆλ‹€. 뒀에 valueλ‘œλŠ” μ›ν•˜λŠ” 검색 쑰건에 따라 μ—¬λŸ¬ κΈ°λŠ₯을 μ œκ³΅ν•©λ‹ˆλ‹€.
μ—¬λŸ¬ κΈ°λŠ₯에 λŒ€ν•œ μ •λ³΄λŠ” μ•„λž˜ 링크λ₯Ό μ°Έκ³ ν•΄μ£Όμ„Έμš”.

https://programmers.co.kr/learn/courses/30/lessons/59047

www.w3schools.com/sql/sql_like.asp

 

SQL LIKE Operator

SQL LIKE Operator The SQL LIKE Operator The LIKE operator is used in a WHERE clause to search for a specified pattern in a column. There are two wildcards often used in conjunction with the LIKE operator:  The percent sign (%) represents zero, one, or mul

www.w3schools.com

 

REGEXPλŠ” μ •κ·œ ν‘œν˜„μ‹(Regular Expression)을 μ΄μš©ν•΄ κ²€μƒ‰ν•©λ‹ˆλ‹€.
LIKEλ₯Ό μ—¬λŸ¬λ²ˆ μ‚¬μš©ν•΄μ•Όν•˜λŠ” μƒν™©μ—μ„œ μœ μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€.
IF()와 μ‘°ν•©ν•˜λ©΄ μ•„λž˜μ™€ 같이 μ‚¬μš©ν•  μˆ˜λ„ 있죠.
μ •κ·œ ν‘œν˜„μ‹μ— λŒ€ν•œ 것은 μ—¬κΈ°μ„œ 닀루지 μ•Šκ² μŠ΅λ‹ˆλ‹€.

programmers.co.kr/learn/courses/30/lessons/59409


- IF(), IFNULL()

IF(쑰건, 참일 λ•Œ λ°˜ν™˜ κ°’, 거짓일 λ•Œ λ°˜ν™˜ κ°’)

IFNULL(value, valueκ°€ NULL이라면 λŒ€μ²΄ κ°’)

https://programmers.co.kr/learn/courses/30/lessons/59410


- WITH RECURSIVE CTE AS, UNION

WITH ν…Œμ΄λΈ”λͺ… AS
(
	SELECT ...
)

μœ„μ™€ 같이 WITH ν‚€μ›Œλ“œλ‘œ κ°€μƒμ˜ ν…Œμ΄λΈ”μ„ λ§Œλ“€ 수 μžˆμŠ΅λ‹ˆλ‹€.

RECURSIVEλŠ” μž¬κ·€μ μœΌλ‘œ μž‘λ™ν•œλ‹€λŠ” 의미이며
μ•„λž˜μ™€ 같이 HOUR이 0~23κΉŒμ§€ μ‘΄μž¬ν•˜λŠ” 가상 ν…Œμ΄λΈ” TIME을 λ§Œλ“€μ–΄ ν™œμš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

UNION ν‚€μ›Œλ“œλŠ” μ‰½κ²Œ 말해 κ²€μƒ‰ν•œ 결과값을 ν•©μ³μ€λ‹ˆλ‹€. μ΄λ•Œ 칼럼 λͺ…을 λ§žμΆ”μ–΄ μ£Όμ–΄μ•Όν•˜λŠ” 점만 μ£Όμ˜ν•©λ‹ˆλ‹€.

https://programmers.co.kr/learn/courses/30/lessons/59413

 

λ°˜μ‘ν˜•

'PS' μΉ΄ν…Œκ³ λ¦¬μ˜ λ‹€λ₯Έ κΈ€

μ½”λ”© ν…ŒμŠ€νŠΈλ₯Ό μœ„ν•œ MySQL 정리 #1 핡심 ν‚€μ›Œλ“œ  (0) 2021.03.18