Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
161 changes: 161 additions & 0 deletions queries.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
/*
다음 경우들에 대해 총 14개의 SQL 쿼리를 작성해 주세요.
예시로 값이 필요한 경우 적당한 값으로 채워넣어서 작성하면 됩니다.
*/

/*
1. 내 정보 업데이트 하기
- 닉네임을 "test"로 업데이트
- 현재 로그인한 유저 id가 1이라고 가정
*/
UPDATE users
SET nickname = "test", updated_at = NOW()
WHERE id = 1;


/*
2. 내가 생성한 상품 조회
- 현재 로그인한 유저 id가 1이라고 가정
- 최신 순으로 정렬
- 10개씩 페이지네이션, 3번째 페이지
*/
SELECT
id,
name,
description,
price,
created_at,
updated_at,
(SELECT COUNT(id) FROM user_favorite_products
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

user_favorite_products 테이블은 존재하지 않는거 같은데 어떤 테이블일까요?

WHERE product_id = products.id) as favorite_count
FROM products
WHERE owner_id = 1
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

owner_id라는 컬럼이 있나요?

ORDER BY created_at DESC
LIMIT 10
OFFSET 20;

/*
3. 내가 생성한 상품의 총 개수
*/
SELECT COUNT(id) as total_count
FROM products
WHERE owner_id = 1;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

owner_id가 아닌거 같아요



/*
4. 내가 좋아요 누른 상품 조회
- 현재 로그인한 유저 id가 1이라고 가정
- 최신 순으로 정렬
- 10개씩 페이지네이션, 3번째 페이지
*/
SELECT
id,
name,
description,
price,
created_at,
updated_at,
(SELECT COUNT(id)
FROM user_favorite_products
WHERE product_id = products.id) as favorite_count
FROM products
WHERE id IN ( SELECT product_id FROM user_favorite_products WHERE user_id = 1)
ORDER BY created_at DESC
LIMIT 10
OFFSET 20;

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

user_favorite_products 테이블을 다른 이름으로 바꿔야 할 것 같아요

/*
5. 내가 좋아요 누른 상품의 총 개수
*/
SELECT
COUNT(id) as total_count
FROM products
WHERE id IN (SELECT product_id
FROM user_favorite_products
WHERE user_id = 1);

/*
6. 상품 생성
- 현재 로그인한 유저 id가 1이라고 가정
*/
INSERT INTO products ( name, description, price, owner_id)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image가 not null로 알고 있어요

VALUES ( "test", "test description", 100, 1 );

/*
7. 상품 목록 조회
- "test" 로 검색
- 최신 순으로 정렬
- 10개씩 페이지네이션, 1번째 페이지
- 각 상품의 좋아요 개수를 포함해서 조회하기
*/
SELECT id, name, description, price, created_at, updated_at,
(SELECT COUNT(id) FROM user_favorite_products WHERE product_id = products.id) as favorite_count
FROM products
WHERE name LIKE '%test%'
ORDER BY created_at DESC
LIMIT 10
OFFSET 0;

/*
8. 상품 상세 조회
- 1번 상품 조회
*/
SELECT
id,
name,
description,
price,
created_at,
updated_at
FROM products
WHERE id = 1;

/*
9. 상품 수정
- 1번 상품 수정
*/
UPDATE products
SET description = "test description2",
updated_at = NOW()
WHERE id = 1;

/*
10. 상품 삭제
- 1번 상품 삭제
*/
DELETE FROM products
WHERE id = 1;

/*
11. 상품 좋아요
- 1번 유저가 2번 상품 좋아요
*/
INSERT INTO user_favorite_products ( user_id, product_id)
VALUES ( 1, 2 );

/*
12. 상품 좋아요 취소
- 1번 유저가 2번 상품 좋아요 취소
*/
DELETE FROM user_favorite_products
WHERE user_id = 1
AND product_id = 2;

/*
13. 상품 댓글 작성
- 1번 유저가 2번 상품에 댓글 작성
*/
INSERT INTO comments ( content, product_id, writer_id)
VALUES ( "test comment", 2, 1 );

/*
14. 상품 댓글 조회
- 1번 상품에 달린 댓글 목록 조회
- 최신 순으로 정렬
- 댓글 날짜 2025-03-25 기준으로 커서 페이지네이션
- 10개씩 페이지네이션
*/
SELECT
id, content, created_at, updated_at, writer_id, product_id
FROM comments WHERE product_id = 1 AND created_at > '2025-03-25'
ORDER BY created_at DESC LIMIT 10;
73 changes: 73 additions & 0 deletions schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// 게시글 등록시 제목 내용 이미지
// 게시글 좋아요 댓글 가능
// 유저 로그인시 이메일 닉네임 비밀번호
// 상품 등록 이미지 상품명 상품소개 판매가격 태그
// 상품 조회시 좋아요 댓글 가능


CREATE TABLE users (
id SERIAL PRIMARY KEY,
email VARCHAR(200) UNIQUE NOT NULL,
nickname VARCHAR(20) UNIQUE NOT NULL,
password VARCHAR(50) NOT NULL,
created_id TIMESTAMP DEFAULT NOW(),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

created_id가 아닐거라고 생각이 들어요

update_id TIMESTAMP DEFAULT NOW()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이것도 id가 아니라 at으로 바꿔야 할 것 같아요

)

CREATE TABLE products (
id SERIAL PRIMARY KEY,
name VARCHAR(100) NOT NULL,
description TEXT,
price BIGINT,
image TEXT NOT NULL,
created_id TIMESTAMP DEFAULT NOW(),
update_id TIMESTAMP DEFAULT NOW(),
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
)

CREATE TABLE tags (
id SERIAL PRIMARY KEY,
name VARCHAR(20) UNIQUE NOT NULL,
created_at TIMESTAMP DEFAULT now() NOT NULL,
updated_at TIMESTAMP DEFAULT now() NOT NULL
);

CREATE TABLE articles (
id SERIAL PRIMARY KEY,
title VARCHAR(200),
description TEXT,
image TEXT,
tag VARCHAR(100),
created_id TIMESTAMP DEFAULT NOW(),
update_id TIMESTAMP DEFAULT NOW()
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
)

CREATE TABLE comments (
id SERIAL PRIMARY KEY,
content TEXT NOT NULL,
created_at TIMESTAMP DEFAULT now() NOT NULL,
updated_at TIMESTAMP DEFAULT now() NOT NULL,
writer_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
article_id INTEGER REFERENCES articles(id) ON DELETE CASCADE,
product_id INTEGER REFERENCES products(id) ON DELETE CASCADE
);

CREATE TABLE articles_likes (
user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
article_id INTEGER NOT NULL REFERENCES articles(id) ON DELETE CASCADE,
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
UNIQUE (user_id, article_id)
)

CREATE TABLE product_likes (
user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
product_id INTEGER NOT NULL REFERENCES products(id) ON DELETE CASCADE,
PRIMARY KEY (user_id, product_id)
);

CREATE TABLE product_tags (
product_id INTEGER NOT NULL REFERENCES products(id) ON DELETE CASCADE,
tag_id INTEGER NOT NULL REFERENCES tags(id) ON DELETE CASCADE,
PRIMARY KEY (product_id, tag_id)
);