MySQL 쿼리문 작성 시,

WHERE절 Subquery활용을 통한 카테고리 별 최대값 찾기 예시 쿼리문을 기록합니다.

 

해당 쿼리문은 하기와 같습니다.

SELECT CATEGORY, NUMBER FROM TABLE
WHERE NUMBER IN (
	SELECT MAX(NUMBER) FROM TABLE
	GROUP BY CATEGORY
	)
GROUP BY CATEGORY

'웹개발자공부 > MySQL' 카테고리의 다른 글

MySQL - COUNT(*), COUNT(COLUMN)의 차이  (0) 2023.02.15
MySQL - WITH와 Subquery의 정의  (0) 2023.02.15
MySQL - IF함수의 정의  (0) 2023.02.15
MySQL - DATE_FORMAT함수의 정의  (0) 2023.02.15
MySQL - DATEDIFF함수의 정의  (0) 2023.02.15

MySQL의 집계함수 COUNT를 사용할 시,

*로 모두 사용할 때와 일반 COLUMN을 지정 할 때에는 하기와 같은 차이가 있습니다.

 

• COUNT(*)

NULL값이 포함되어 있는 DATA까지 집계합니다.

 

• COUNT(COLUMN)

NULL값이 포함되어 있는 DATA는 집계하지 않습니다.

 

실제 테스트 결과를 첨부합니다.

description COLUMN이 모두 NULL임

 

MySQL의 WITH와 Subquery의 정의는 각각 하기와 같습니다.

MySQL 공식 문서를 참고하였습니다.

 

https://dev.mysql.com/doc/refman/8.0/en/with.html

 

MySQL :: MySQL 8.0 Reference Manual :: 13.2.20 WITH (Common Table Expressions)

13.2.20 WITH (Common Table Expressions) A common table expression (CTE) is a named temporary result set that exists within the scope of a single statement and that can be referred to later within that statement, possibly multiple times. The following disc

dev.mysql.com

https://dev.mysql.com/doc/refman/8.0/en/subqueries.html

 

MySQL :: MySQL 8.0 Reference Manual :: 13.2.15 Subqueries

A subquery is a SELECT statement within another statement. All subquery forms and operations that the SQL standard requires are supported, as well as a few features that are MySQL-specific. Here is an example of a subquery: SELECT * FROM t1 WHERE column1 =

dev.mysql.com

 

• WITH (Common Table Expressions)

A common table expression (CTE) is a named temporary result set that exists within the scope of a single statement and that can be referred to later within that statement, possibly multiple times.

쿼리문 내에서 존재하는 임시 Table이란 점이 특징입니다.

 

Subqueries

A subquery is a SELECT statement within another statement.

다른 쿼리문 내에 존재하는 SELECT쿼리문입니다.

 

대체로 WHERE문에서 많이 사용되는,

단일 값을 나타내는 Subquery를 Scalar Subquery라고도 부릅니다.

 

 

MySQL의 조건문 관련 문법 중 IF함수의 정의는 하기와 같습니다.

MySQL 공식 문서를 참고하였습니다.

https://dev.mysql.com/doc/refman/8.0/en/flow-control-functions.html#function_if

 

MySQL :: MySQL 8.0 Reference Manual :: 12.5 Flow Control Functions

12.5 Flow Control Functions Table 12.7 Flow Control Operators Name Description CASE Case operator IF() If/else construct IFNULL() Null if/else construct NULLIF() Return NULL if expr1 = expr2 CASE value WHEN compare_value THEN result [WHEN compare_value

dev.mysql.com

 

• IF(expr1,expr2,expr3)

expr1이 TRUE라면 expr2를 반환하고, 그렇지 않다면 expr3을 반환합니다.

MySQL의 날짜관련 문법 중 DATE_FORMAT함수의 정의는 하기와 같습니다.

MySQL 공식 문서를 참고하였습니다.

https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_date-format

 

MySQL :: MySQL 8.0 Reference Manual :: 12.7 Date and Time Functions

12.7 Date and Time Functions This section describes the functions that can be used to manipulate temporal values. See Section 11.2, “Date and Time Data Types”, for a description of the range of values each date and time type has and the valid formats

dev.mysql.com

 

• DATE_FORMAT(date,format)

인자로 삽입 된 date의 형식을 지정한 format으로 변경하여 출력합니다.

YYYY-MM-DD형식으로 나타내기 위해서는 '%Y-%m-%d'로 기입합니다.

MySQL의 날짜관련 문법 중 DATEDIFF함수의 정의는 하기와 같습니다.

MySQL 공식 문서를 참고하였습니다.

https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_datediff

 

MySQL :: MySQL 8.0 Reference Manual :: 12.7 Date and Time Functions

12.7 Date and Time Functions This section describes the functions that can be used to manipulate temporal values. See Section 11.2, “Date and Time Data Types”, for a description of the range of values each date and time type has and the valid formats

dev.mysql.com

 

• DATEDIFF(expr1,expr2)

expr1과 expr2의 차이를 date로 나타냅니다.

DATEDIFF이외에도 TIMEDIFF, TIMESTAMPDIFF, PERIOD_DIFF 등이 있습니다.

MySQL의 NULL관련 문법 중 IFNULL과 NULLIF의 정의는 하기와 같습니다.

MySQL 공식 문서를 참고하였습니다.

https://dev.mysql.com/doc/refman/8.0/en/flow-control-functions.html#function_ifnull

 

MySQL :: MySQL 8.0 Reference Manual :: 12.5 Flow Control Functions

12.5 Flow Control Functions Table 12.7 Flow Control Operators Name Description CASE Case operator IF() If/else construct IFNULL() Null if/else construct NULLIF() Return NULL if expr1 = expr2 CASE value WHEN compare_value THEN result [WHEN compare_value

dev.mysql.com

 

• IFNULL(expr1, expr2)

expr1이 NULL이 아니라면 expr1을 반환하고, NULL이라면 expr2를 반환합니다.

 

• NULLIF(expr1, expr2)

expr1과 expr2의 결과값이 같을 경우 NULL을 반환하고, 그렇지 않다면 expr1을 반환합니다.

 

 

로컬이 아닌 원격 서버에 접속 할 경우,

mysql -h <mysql아이피 및 end point> -u <계정명> -p

를 사용하여 접속합니다.

#database

DATABASE_URL = "mysql://[AWS RDS ID(root or admin)]:[해당 비밀번호]@[AWS RDS endpoint]:[PORT]/[db name]"

 

#typeorm

TYPEORM_CONNECTION = mysql

TYPEORM_HOST = [AWS RDS endpoint]

TYPEORM_DATABASE = [db name]

TYPEORM_PORT = 3306

TYPEORM_USERNAME = [AWS RDS ID(root or admin)]

TYPEORM_PASSWORD = [해당 비밀번호]

TYPEORM_LOGGING = TYPEORM_USERNAME

 

DATABASE_URL, TYPEORM_HOST 등과 같이 진하게 표시 한 부분을 주의합니다.

MySQL Int형 데이터의 크기에 대해 정리합니다.

 

MySQL의 Int형 데이터에 대한 전반적인 내용은 MySQL공식홈페이지에서,

https://dev.mysql.com/doc/refman/8.0/en/integer-types.html

 

MySQL :: MySQL 8.0 Reference Manual :: 11.1.2 Integer Types (Exact Value) - INTEGER, INT, SMALLINT, TINYINT, MEDIUMINT,

11.1.2 Integer Types (Exact Value) - INTEGER, INT, SMALLINT, TINYINT, MEDIUMINT, BIGINT MySQL supports the SQL standard integer types INTEGER (or INT) and SMALLINT. As an extension to the standard, MySQL also supports the integer types TINYINT, MEDIUMINT,

dev.mysql.com

 

Unsigned Int형에 관한 내용은 하기 홈페이지를 참고하였습니다. 

https://www.cs.utah.edu/~germain/PPS/Topics/unsigned_integer.html

 

Programming - Unsigned Integers

Unsigned Integers Unsigned integers are used when we know that the value that we are storing will always be non-negative (zero or positive). Note: it is almost always the case that you could use a regular integer variable in place of an unsigned integer. T

www.cs.utah.edu

 

Type Storage ( Bytes) Minimum Value
Signed
Minimum Value
Unsigned
Maximum Value
Signed
Maximum Value
Unsigned
TINYINT 1 -128 0 127 255
SMALLINT 2 -32768 0 32767 65535
MEDIUMINT 3 -8388608 0 8388607 16777215
INT 4 -2147483648 0 2147483647 4294967295
BIGINT 8 -2^63 0 2(^63)-1 2(^64)-1

 

위 표와 링크를 통해 하기 2가지 내용에 대해 알 수 있습니다.

 

1. TINY는 최대(unsigned의 경우에 한정) 255까지 표현 할 수 있다.

-> 각 타입별로 데이터할당 크기를 정확히 파악 할 필요가 있겠습니다.

 

2. 부호의 유무(signed, unsigned)에 따라 최대 데이터 표현 크기가 다르다.

-> 음의정수를 표현필요 유무를 고려하여 데이터타입을 지정 할 필요가 있겠습니다.

+ Recent posts