concat 문자열 합치기
https://mariadb.com/kb/en/concat/
syntax
CONCAT(str1,str2,...)
example
SELECT CONCAT('Ma', 'ria', 'DB');
+---------------------------+
| CONCAT('Ma', 'ria', 'DB') |
+---------------------------+
| MariaDB |
+---------------------------+
SELECT CONCAT('Ma', 'ria', NULL, 'DB');
+---------------------------------+
| CONCAT('Ma', 'ria', NULL, 'DB') |
+---------------------------------+
| NULL |
+---------------------------------+
SELECT CONCAT(42.0);
+--------------+
| CONCAT(42.0) |
+--------------+
| 42.0 |
+--------------+
concat_ws 여러 컬럼값을 연결해서 하나의 컬럼값으로 출력하기 (feat. 중간 문자 삽입)
syntax
CONCAT_WS(separator,str1,str2,...)
example
SELECT CONCAT_WS(',','First name','Second name','Last Name');
+-------------------------------------------------------+
| CONCAT_WS(',','First name','Second name','Last Name') |
+-------------------------------------------------------+
| First name,Second name,Last Name |
+-------------------------------------------------------+
SELECT CONCAT_WS('-','Floor',NULL,'Room');
+------------------------------------+
| CONCAT_WS('-','Floor',NULL,'Room') |
+------------------------------------+
| Floor-Room |
+------------------------------------+
group_concat 여러줄을 한 컬럼값으로 출력하기
https://mariadb.com/kb/en/group_concat/
syntax
GROUP_CONCAT(expr)
GROUP_CONCAT([DISTINCT] expr [,expr ...]
[ORDER BY {unsigned_integer | col_name | expr}
[ASC | DESC] [,col_name ...]]
[SEPARATOR str_val]
[LIMIT {[offset,] row_count | row_count OFFSET offset}])
LIMIT
Until MariaDB 10.3.2, it was not possible to use the LIMIT clause with GROUP_CONCAT. This restriction was lifted in MariaDB 10.3.3.
example
SELECT student_name,
GROUP_CONCAT(test_score)
FROM student
GROUP BY student_name;
Get a readable list of MariaDB users from the mysql.user table:
SELECT GROUP_CONCAT(DISTINCT User ORDER BY User SEPARATOR '\n')
FROM mysql.user;
In the former example, DISTINCT is used because the same user may occur more than once. The new line (\n) used as a SEPARATOR makes the results easier to read.
Get a readable list of hosts from which each user can connect:
SELECT User, GROUP_CONCAT(Host ORDER BY Host SEPARATOR ', ')
FROM mysql.user GROUP BY User ORDER BY User;
The former example shows the difference between the GROUP_CONCAT's ORDER BY (which sorts the concatenated hosts), and the SELECT's ORDER BY (which sorts the rows).
From MariaDB 10.3.3, LIMIT can be used with GROUP_CONCAT, so, for example, given the following table:
CREATE TABLE d (dd DATE, cc INT);
INSERT INTO d VALUES ('2017-01-01',1);
INSERT INTO d VALUES ('2017-01-02',2);
INSERT INTO d VALUES ('2017-01-04',3);
the following query:
SELECT SUBSTRING_INDEX(GROUP_CONCAT(CONCAT_WS(":",dd,cc) ORDER BY cc DESC),",",1) FROM d;
+----------------------------------------------------------------------------+
| SUBSTRING_INDEX(GROUP_CONCAT(CONCAT_WS(":",dd,cc) ORDER BY cc DESC),",",1) |
+----------------------------------------------------------------------------+
| 2017-01-04:3 |
+----------------------------------------------------------------------------+
can be more simply rewritten as:
SELECT GROUP_CONCAT(CONCAT_WS(":",dd,cc) ORDER BY cc DESC LIMIT 1) FROM d;
+-------------------------------------------------------------+
| GROUP_CONCAT(CONCAT_WS(":",dd,cc) ORDER BY cc DESC LIMIT 1) |
+-------------------------------------------------------------+
| 2017-01-04:3 |
+-------------------------------------------------------------+
'DB(SQL) > mysql|maria' 카테고리의 다른 글
[mariadb] mariadb version 선택시 참조해야하는 글 (0) | 2022.06.21 |
---|---|
[mysql] mysql version 선택시 참조해야하는 글 (0) | 2022.06.21 |
[MariaDB] 오픈소스 모니터링 툴 PMM2 사용기 (0) | 2022.05.31 |
[Mariadb] Window functions not working in ONLY_FULL_GROUP_BY mode II (0) | 2022.05.20 |
(펌)MySQL 쓰면서 하지 말아야 할 것 17가지 (0) | 2022.05.04 |