๐งท CONCAT
์ฌ๋ฌ๋ฌธ์์ด์ ํ๋๋ก ํฉ์น๊ฑฐ๋ ์ฐ๊ฒฐ (select ๋ค์ ์ฌ์ฉ)SELECT CONCAT ('string1','string2');
mysql> SELECT CONCAT ('์ด๋ฆ:',name ) as '์ ๋ณด'
-> FROM celeb;
+------------------+
| ์ ๋ณด |
+------------------+
| ์ด๋ฆ:์์ด์ |
| ์ด๋ฆ:์ด๋ฏธ์ฃผ |
| ์ด๋ฆ:์ก๊ฐ |
| ์ด๋ฆ:๊ฐ๋์ |
| ์ด๋ฆ:์ ์ฌ์ |
| ์ด๋ฆ:์ฐจ์น์ |
| ์ด๋ฆ:์ด์ํ |
+------------------+
7 rows in set (0.00 sec)
๐งท ALIAS (as)
๋ณ์นญ ์์ฑ (์ปฌ๋ผ์ด๋, ํ ์ด๋ธ ์ด๋ฆ์ ๋ณ์นญ์์ฑ)SELECT column as '๋ณ๋ช ' FROM tablename as '๋ณ๋ช '
์์ด๋ ' ' (๋ฐ์ดํ) ์๋ต๊ฐ๋ฅ
as ์๋ต๊ฐ๋ฅ
๐ name ๊ณผ job_title ์ ํฉ์ณ์ profile ์ด๋ผ๋ ๋ณ์นญ์ ๋ง๋ค์ด์ ๊ฒ์
mysql> SELECT CONCAT ( name, ':' , job_title) as profile
-> FROM celeb;
+-----------------------------------+
| profile |
+-----------------------------------+
| ์์ด์ :๊ฐ์, ํค๋ฐํธ |
| ์ด๋ฏธ์ฃผ:๊ฐ์ |
| ์ก๊ฐ:ํค๋ฐํธ |
| ๊ฐ๋์:์ํ๋ฐฐ์ฐ, ํค๋ฐํธ |
| ์ ์ฌ์:MC, ๊ฐ๊ทธ๋งจ |
| ์ฐจ์น์:์ํ๋ฐฐ์ฐ, ๋ชจ๋ธ |
| ์ด์ํ:๊ฐ์ |
+-----------------------------------+
7 rows in set (0.00 sec)
๐snl_korea ์ ์ถ์ฐํ celeb ์ ๊ธฐ์ค์ผ๋ก ๋ ํ
์ด๋ธ์ ์กฐ์ธํ์ฌ,
celeb ํ
์ด๋ธ์ c, snl_show ํ
์ด๋ธ์ s ๋ผ๋ ๋ณ์นญ์ ๋ง๋ค์ด์ ์ถ์ฐํ ์์ฆ๊ณผ ์ํผ์๋, ์ด๋ฆ, ์ง์
์ ๊ฒ์
mysql> SELECT s.season, s.episode, c.name, c.job_title
-> FROM celeb c, snl_show s
-> WHERE c.name=s.host;
+--------+---------+-----------+-------------------------+
| season | episode | name | job_title |
+--------+---------+-----------+-------------------------+
| 8 | 7 | ๊ฐ๋์ | ์ํ๋ฐฐ์ฐ, ํค๋ฐํธ |
| 8 | 8 | ์ ์ฌ์ | MC, ๊ฐ๊ทธ๋งจ |
| 8 | 9 | ์ฐจ์น์ | ์ํ๋ฐฐ์ฐ, ๋ชจ๋ธ |
| 8 | 10 | ์ด์ํ | ๊ฐ์ |
+--------+---------+-----------+-------------------------+
4 rows in set (0.00 sec)
๐snl_korea ์ ์ถ์ฐํ celeb ์ ๊ธฐ์ค์ผ๋ก ๋ ํ
์ด๋ธ์ ์กฐ์ธํ์ฌ ๋ค์๊ณผ ๊ฐ์ด ๊ฐ ๋ฐ์ดํฐ์ ๋ณ์นญ์ ์ฌ์ฉํ์ฌ ๊ฒ์
โข ์์ฆ, ์ํผ์๋, ๋ฐฉ์ก์ผ์ ํฉ์ณ์ โ๋ฐฉ์ก์ ๋ณดโ
โข ์ด๋ฆ, ์ง์
์ ํฉ์ณ์ โ์ถ์ฐ์์ ๋ณดโ
mysql> SELECT CONCAT(s.season, '-', s.episode, '(', s.broadcast_date, ')') as '๋ฐฉ์ก์ ๋ณด',
-> CONCAT(c.name, '(', c.job_title, ')' ) as '์ถ์ฐ์์ ๋ณด'
-> FROM snl_show as s , celeb as c
-> WHERE s.host = c.name;
+------------------+------------------------------------+
| ๋ฐฉ์ก์ ๋ณด | ์ถ์ฐ์์ ๋ณด |
+------------------+------------------------------------+
| 8-7(2020-09-05) | ๊ฐ๋์(์ํ๋ฐฐ์ฐ, ํค๋ฐํธ) |
| 8-8(2020-09-12) | ์ ์ฌ์(MC, ๊ฐ๊ทธ๋งจ) |
| 8-9(2020-09-19) | ์ฐจ์น์(์ํ๋ฐฐ์ฐ, ๋ชจ๋ธ) |
| 8-10(2020-09-26) | ์ด์ํ(๊ฐ์) |
+------------------+------------------------------------+
4 rows in set (0.00 sec)
๐
mysql> SELECT CONCAT ('์ด ๋ฆ : ',name, ', ์ ์ ์ฌ : ', agency) as '์ฐ ์ ์ธ ์ ๋ณด'
-> FROM celeb
-> WHERE name like '___';
+-------------------------------------------------------------+
| ์ฐ ์ ์ธ ์ ๋ณด |
+-------------------------------------------------------------+
| ์ด ๋ฆ : ์์ด์ , ์ ์ ์ฌ : EDAM์ํฐํ
์ด๋จผํธ |
| ์ด ๋ฆ : ์ด๋ฏธ์ฃผ, ์ ์ ์ฌ : ์ธ๋ฆผ์ํฐํ
์ด๋จผํธ |
| ์ด ๋ฆ : ๊ฐ๋์, ์ ์ ์ฌ : YG์ํฐํ
์ด๋จผํธ |
| ์ด ๋ฆ : ์ ์ฌ์, ์ ์ ์ฌ : ์ํ
๋ |
| ์ด ๋ฆ : ์ฐจ์น์, ์ ์ ์ฌ : YG์ํฐํ
์ด๋จผํธ |
| ์ด ๋ฆ : ์ด์ํ, ์ ์ ์ฌ : YG์ํฐํ
์ด๋จผํธ |
+-------------------------------------------------------------+
6 rows in set (0.00 sec)
๐
mysql> SELECT agency as '์์์ฌ์ ๋ณด',
-> CONCAT( '๋์ด : ', age, '(', sex, ')') as '์ ์์ ๋ณด'
-> ,CONCAT( season, '-', episode, ', ๋ฐฉ์ก๋ ์ง : ', broadcast_date) as '์ถ์ฐ์ ๋ณด'
-> FROM celeb as c, snl_show as s
-> WHERE name = host
-> AND agency like '__์ํฐํ
์ด๋จผํธ'
-> ORDER BY broadcast_date DESC;
+----------------------+----------------+---------------------------------+
| ์์์ฌ์ ๋ณด | ์ ์์ ๋ณด | ์ถ์ฐ์ ๋ณด |
+----------------------+----------------+---------------------------------+
| YG์ํฐํ
์ด๋จผํธ | ๋์ด : 23(F) | 8-10, ๋ฐฉ์ก๋ ์ง : 2020-09-26 |
| YG์ํฐํ
์ด๋จผํธ | ๋์ด : 48(M) | 8-9, ๋ฐฉ์ก๋ ์ง : 2020-09-19 |
| YG์ํฐํ
์ด๋จผํธ | ๋์ด : 41(M) | 8-7, ๋ฐฉ์ก๋ ์ง : 2020-09-05 |
+----------------------+----------------+---------------------------------+
3 rows in set (0.01 sec)
๐งท DISTINCT
๊ฒ์๊ฒฐ๊ณผ ์ค๋ณต์ ๊ฑฐ (select๋ฌธ์์ ์ฌ์ฉ)SELECT DISTINCT column FROM tablename ;
๐ ์์
mysql> SELECT DISTINCT agency, sex
-> FROM celeb;
+--------------------------+------+
| agency | sex |
+--------------------------+------+
| EDAM์ํฐํ
์ด๋จผํธ | F |
| ์ธ๋ฆผ์ํฐํ
์ด๋จผํธ | F |
| ๋๋ฌด์ํฐ์ค | M |
| YG์ํฐํ
์ด๋จผํธ | M |
| ์ํ
๋ | M |
| YG์ํฐํ
์ด๋จผํธ | F |
+--------------------------+------+
6 rows in set (0.00 sec)
โก๏ธ ์ปฌ๋ผ ๋๊ฐ๋ฅผ ๋ฃ์์ ๋ ๊ฒฐ๊ณผ๊ฐ ๊ถ๊ธํด์ ๋๊ฐ์ง๋ฅผ ๋ฃ์ด๋ณด์๋ค.
agency๋ง ๋ฃ์๋ค๋ฉด ๋ง์ง๋ง ์ค์ธ YG์ํฐํ
์ด๋จผํธ๋ ์ถ๋ ฅ๋์ง ์๊ฒ ์ง๋ง,
์ค๋ช
์ปฌ๋ผ์ ์ถ๊ฐํ์๋๋ ๋ค๋ฅธ ์ฑ๋ณ์ ๊ฐ์ง ๋ฐ์ดํฐ๋ ํจ๊ป ์ถ๋ ฅ๋จ์ ์ ์ ์๋ค.
๐celeb ํ ์ด๋ธ์์ ์ฑ๋ณ๊ณผ ์์์ฌ ๋ณ ์ข ๋ฅ๋ฅผ ๊ฒ์ํ์ฌ ์ฑ๋ณ, ์์์ฌ ์์ผ๋ก ์ ๋ ฌํ์ธ์.
mysql> SELECT DISTINCT sex, agency
-> FROM celeb
-> ORDER BY sex, agency;
+------+--------------------------+
| sex | agency |
+------+--------------------------+
| F | EDAM์ํฐํ
์ด๋จผํธ |
| F | YG์ํฐํ
์ด๋จผํธ |
| F | ์ธ๋ฆผ์ํฐํ
์ด๋จผํธ |
| M | YG์ํฐํ
์ด๋จผํธ |
| M | ๋๋ฌด์ํฐ์ค |
| M | ์ํ
๋ |
+------+--------------------------+
6 rows in set (0.00 sec)
๐งท LIMIT
๊ฒ์๊ฒฐ๊ณผ๋ฅผ ์ ๋ ฌ๋ ์์ผ๋ก ์ฃผ์ด์ง ์ซ์๋งํผ๋ง ์กฐํ (์ฟผ๋ฆฌ ๊ฐ์ฅ ๋ง์ง๋ง์ ์ฌ์ฉ)SELECT column1, column2, ... FROM tablename WHERE condition LIMIT number;
๐ ๋์ด๊ฐ ๊ฐ์ฅ ์ ์ ์ฐ์์ธ 4๋ช ์ ๊ฒ์
mysql> SELECT * FROM celeb
-> ORDER BY age
-> LIMIT 4;
+----+-----------+------------+------+------+-------------------+--------------------------+
| ID | NAME | BIRTHDAY | AGE | SEX | JOB_TITLE | AGENCY |
+----+-----------+------------+------+------+-------------------+--------------------------+
| 7 | ์ด์ํ | 1999-05-04 | 23 | F | ๊ฐ์ | YG์ํฐํ
์ด๋จผํธ |
| 2 | ์ด๋ฏธ์ฃผ | 1994-09-23 | 28 | F | ๊ฐ์ | ์ธ๋ฆผ์ํฐํ
์ด๋จผํธ |
| 3 | ์ก๊ฐ | 1994-04-23 | 28 | M | ํค๋ฐํธ | ๋๋ฌด์ํฐ์ค |
| 1 | ์์ด์ | 1993-05-16 | 29 | F | ๊ฐ์, ํค๋ฐํธ | EDAM์ํฐํ
์ด๋จผํธ |
+----+-----------+------------+------+------+-------------------+--------------------------+
4 rows in set (0.00 sec)
๐celeb ํ ์ด๋ธ์์ ๋จ์ ์ฐ์์ธ ์ค ๋์ด๊ฐ ๊ฐ์ฅ ๋ง์ 2๋ช ์ ์กฐํํ์ธ์
mysql> SELECT * FROM celeb
-> WHERE sex = 'M'
-> ORDER BY age DESC
-> LIMIT 2;
+----+-----------+------------+------+------+----------------------+----------------------+
| ID | NAME | BIRTHDAY | AGE | SEX | JOB_TITLE | AGENCY |
+----+-----------+------------+------+------+----------------------+----------------------+
| 5 | ์ ์ฌ์ | 1972-08-14 | 50 | M | MC, ๊ฐ๊ทธ๋งจ | ์ํ
๋ |
| 6 | ์ฐจ์น์ | 1970-06-07 | 48 | M | ์ํ๋ฐฐ์ฐ, ๋ชจ๋ธ | YG์ํฐํ
์ด๋จผํธ |
+----+-----------+------------+------+------+----------------------+----------------------+
2 rows in set (0.00 sec)
๐
mysql> SELECT CONCAT ('SNL ์์ฆ ', season, ' ํธ์คํธ ', host) as 'SNL ๋ฐฉ์ก์ ๋ณด'
-> , age
-> FROM snl_show, celeb
-> WHERE name = host
-> AND sex = 'M'
-> ORDER BY age DESC
-> LIMIT 2;
+----------------------------------+------+
| SNL ๋ฐฉ์ก์ ๋ณด | age |
+----------------------------------+------+
| SNL ์์ฆ 8 ํธ์คํธ ์ ์ฌ์ | 50 |
| SNL ์์ฆ 8 ํธ์คํธ ์ฐจ์น์ | 48 |
+----------------------------------+------+
2 rows in set (0.00 sec)