๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

Back-end/DB

[MySQL] ๋ฐ์ดํ„ฐ๋ฅผ ์˜ค๋ฆ„์ฐจ์ˆœ, ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•˜๋Š” ๋ฐฉ๋ฒ•

728x90
๋ฐ˜์‘ํ˜•

๐ŸŸก Order by ๋ฌธ์„ ํ™œ์šฉํ•˜์—ฌ ๊ฒ€์ƒ‰๋œ ๋ฐ์ดํ„ฐ๋ฅผ ์ •๋ ฌํ•˜๊ธฐ

 

๐Ÿ–ค Order by ๋ฌธ์€?

ํ…Œ์ด๋ธ”์—์„œ SELECT๋ฌธ์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์กฐํšŒํ•  ๋•Œ, ORDER BY๋ฅผ ์ถ”๊ฐ€ํ•˜์—ฌ ์ง€์ •๋œ ์ปฌ๋Ÿผ์„ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ•  ์ˆ˜ ์žˆ๋‹ค. ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ๊ณผ ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ๋กœ ๋‘ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์ด ์žˆ๋Š”๋ฐ default ๊ฐ’์€ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ์ด๊ธฐ ๋•Œ๋ฌธ์— ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ๋งŒ ๋”ฐ๋กœ ์ ์–ด์ฃผ๋ฉด ๋œ๋‹ค.

+ ์ปฌ๋Ÿผ๋ช…์ด ์•„๋‹Œ ์ปฌ๋Ÿผ ๋ฒˆํ˜ธ๋กœ๋„ ์ •๋ ฌ์„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค!

 

 

๐Ÿ–ค ์‹ค์ „ ์˜ˆ์ œ ํ’€์–ด๋ณด๊ธฐ

https://ye5ni.tistory.com/145
↑↑↑(์œ„ ๋งํฌ ์ฐธ๊ณ ) ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ƒ์„ฑ ํ›„ ํ•ด๋‹นํ•˜๋Š” ํ…Œ์ด๋ธ”์„ ๊ธฐ์ค€์œผ๋กœ ์‹ค์Šต ์ง„ํ–‰

 

โœ… ์กฐ๊ฑด 1. ์‹œ๋„๋ช…(sido)์€ '์„œ์šธํŠน๋ณ„์‹œ', ์‹œ๊ตฐ๊ตฌ๋ช…(gugun)์€ '์ค‘๊ตฌ', ๋™๋ช…(dong)์€ 'ํ•„๋™'์œผ๋กœ ์‹œ์ž‘

โœ… ์กฐ๊ฑด 2. ๊ฒฐ๊ณผ๋Š” ์‹œ๋„๋ช…, ์‹œ๊ตฐ๊ตฌ๋ช…, ์๋ฉด๋™๋ช…, ๋„๋กœ๋ช…์˜ ์ˆœ์œผ๋กœ ์ •๋ ฌ

 

โœ… ์˜ˆ์‹œ 

์ด๋Ÿฐ ํ˜•์‹์œผ๋กœ ์ถœ๋ ฅ๋  ์ˆ˜ ์žˆ๋Š” ์ฟผ๋ฆฌ๋ฅผ ์ž‘์„ฑํ•ด๋ณด์ž!

 

 

์—ฌ๊ธฐ์„œ sido, gugun, dong, road ์ˆœ์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ์„ ํ•ด๋ณผ ๊ฒƒ์ด๋‹ค. 

 

 

์‚ฌ์šฉ๋ฒ•
# ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ
select * from ํ…Œ์ด๋ธ”
order by ์ปฌ๋Ÿผ1 ASC;

-> ์—ฌ๊ธฐ์„œ ASC๋Š” default ๊ฐ’์ด๊ธฐ ๋•Œ๋ฌธ์— ์ƒ๋žต ๊ฐ€๋Šฅ!

# ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ
select * from ํ…Œ์ด๋ธ”
order by ์ปฌ๋Ÿผ1 DESC;
# ์—ฌ๋Ÿฌ ์ปฌ๋Ÿผ์œผ๋กœ ์ •๋ ฌ
select * from ํ…Œ์ด๋ธ”
order by ์ปฌ๋Ÿผ1, ์ปฌ๋Ÿผ2, ์ปฌ๋Ÿผ3;
# ์กฐ๊ฑด์‹์ด ์žˆ๋Š” ๊ฒฝ์šฐ ์ •๋ ฌ
select * from ํ…Œ์ด๋ธ”
where ์กฐ๊ฑด์‹
order by ์ปฌ๋Ÿผ1, ์ปฌ๋Ÿผ2;

 

 

์˜ˆ์ œ ๋ฌธ์ œ์— ์ ์šฉํ•˜๊ธฐ

 

์‹œ๋„๋ช…, ์‹œ๊ตฐ๊ตฌ๋ช…, ์๋ฉด๋™๋ช…, ๋„๋กœ๋ช…์˜ ์ˆœ์œผ๋กœ ์ •๋ ฌ

 

select scd, rno, dno, concat(sido, " ", gugun, " ", dong, " ", road) as juso,
concat(eroad, ",", edong, ",", egugun,",", esido) as ejuso 
from road_code  
where sido="์„œ์šธํŠน๋ณ„์‹œ" and gugun="์ค‘๊ตฌ" and dong like "ํ•„๋™%" 
order by sido, gugun, dong, road;

-> ๋ชจ๋‘ ๋‹ค ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ์ด๊ธฐ ๋•Œ๋ฌธ์— ๋ณ„๋„์˜ ์„ค์ •์€ ํ•„์š”์—†๊ณ , ์ œ์ผ ์šฐ์„ ์‹œํ•ด์„œ ์ •๋ ฌํ•˜๊ณ  ์‹ถ์€ ์ปฌ๋Ÿผ ์ˆœ์„œ๋Œ€๋กœ ์ ์œผ๋ฉด ๋œ๋‹ค.

 

 

 

 

 

order by ์ •๋ ฌ์— ๋Œ€ํ•œ ์ž์„ธํ•œ ์„ค๋ช…์€ โžก๏ธโžก๏ธโžก๏ธ https://ye5ni.tistory.com/149

concat ํ•จ์ˆ˜์— ๋Œ€ํ•œ ์ž์„ธํ•œ ์„ค๋ช…์€ โžก๏ธโžก๏ธโžก๏ธ https://ye5ni.tistory.com/147

728x90
๋ฐ˜์‘ํ˜•