MySQL

MySQL) like์— ๋นจ๊ฐ„ ๋ฐ‘์ค„์ด ๋œฐ ๋•Œ ๋ฐฑํ‹ฑ(`) ์‚ฌ์šฉํ•˜๊ธฐ

567Rabbit 2024. 5. 30. 17:19

 

 

 

like์— ์˜ค๋ฅ˜๊ฐ€ ๋œจ๋Š”๋ฐ, ๋ฐฑํ‹ฑ์„ ์‚ฌ์šฉํ•˜๋ฉด ์˜ค๋ฅ˜๊ฐ€ ๋œจ์ง€ ์•Š๋Š”๋‹ค

 

 

 

 

๋ฐฑํ‹ฑ(`)?

 

์‹๋ณ„์ž(ํ…Œ์ด๋ธ” ์ด๋ฆ„, ์—ด ์ด๋ฆ„ ๋“ฑ)๋ฅผ ๋ช…ํ™•ํžˆ ๊ตฌ๋ถ„ํ•˜์—ฌ SQL ๋ฌธ๋ฒ•์—์„œ ์‹๋ณ„์ž๋ฅผ ๋ช…ํ™•ํ•˜๊ฒŒ ์ •์˜ํ•˜๊ณ  ์ถฉ๋Œ์„ ํ”ผํ•˜๊ธฐ ์œ„ํ•ด ์ค‘์š”ํ•œ ์—ญํ• ์„ ํ•œ๋‹ค.

 

 

 

 

๋ฐฑํ‹ฑ์„ ์‚ฌ์šฉํ•˜๋Š” ์ด์œ 

 

 

1. ์˜ˆ์•ฝ์–ด ์ถฉ๋Œ ๋ฐฉ์ง€: SQL์—๋Š” SELECT, INSERT, UPDATE ๋“ฑ๊ณผ ๊ฐ™์€ ์˜ˆ์•ฝ์–ด๊ฐ€ ๋งŽ๋‹ค. ํ…Œ์ด๋ธ” ์ด๋ฆ„์ด๋‚˜ ์—ด ์ด๋ฆ„์ด ์ด๋Ÿฌํ•œ ์˜ˆ์•ฝ์–ด์™€ ์ถฉ๋Œํ•  ๋•Œ, ๋ฐฑํ‹ฑ์„ ์‚ฌ์šฉํ•˜๋ฉด ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๊ฐ€ ์ด๊ฒƒ์„ ์‹๋ณ„์ž๋กœ ์ธ์‹ํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, like๋Š” ํŒจํ„ด ๋งค์นญ์—์„œ ์‚ฌ์šฉ๋˜๋Š” SQL ์˜ˆ์•ฝ์–ด์ด๋‹ค. ์ด๋ฅผ ํ…Œ์ด๋ธ” ์ด๋ฆ„์œผ๋กœ ์‚ฌ์šฉํ•  ๋•Œ๋Š” ๋ฐฑํ‹ฑ์„ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.

INSERT INTO `like` (postingId, userId) VALUES (2, 1);

 

insert into `like`
(postingId, userId)
values
(2, 1);

select * from `like`;

delete from `like`
where postingId = 2 and userId = 1;

 

 

 

 

2. ํŠน์ˆ˜ ๋ฌธ์ž ์‚ฌ์šฉ: ํ…Œ์ด๋ธ”์ด๋‚˜ ์—ด ์ด๋ฆ„์— ๊ณต๋ฐฑ์ด๋‚˜ ํŠน์ˆ˜ ๋ฌธ์ž๋ฅผ ํฌํ•จํ•˜๊ณ  ์‹ถ์„ ๋•Œ๋„ ๋ฐฑํ‹ฑ์„ ์‚ฌ์šฉํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ์—ด ์ด๋ฆ„์— ๊ณต๋ฐฑ์ด ํฌํ•จ๋œ ๊ฒฝ์šฐ์ด๋‹ค.

SELECT `user name` FROM `user data`;