-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: add the sql queries we used in this project into an md file
- Loading branch information
1 parent
b7a5206
commit 56a0043
Showing
1 changed file
with
93 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
# SQL Queries of our project | ||
## Items | ||
- get all categories | ||
```sql | ||
SELECT * | ||
FROM item_category; | ||
``` | ||
|
||
- create view that reference all items | ||
```sql | ||
CREATE OR REPLACE VIEW all_items AS | ||
SELECT * | ||
FROM item_item; | ||
``` | ||
|
||
- create view that reference to items filtered by category | ||
```sql | ||
CREATE OR REPLACE VIEW filtered_by_category AS | ||
SELECT * | ||
FROM all_items | ||
WHERE category_id = %s; | ||
``` | ||
|
||
- get all items that match the search query | ||
```sql | ||
SELECT * | ||
FROM filtered_by_category | ||
WHERE name LIKE '%s' OR description LIKE '%s'; | ||
``` | ||
|
||
- get all items from 'filtered_by_category' view | ||
```sql | ||
SELECT * | ||
FROM filtered_by_category; | ||
``` | ||
|
||
- get all items that match the search query | ||
```sql | ||
SELECT * | ||
FROM all_items | ||
WHERE name LIKE %s OR description LIKE %s; | ||
``` | ||
|
||
## detail | ||
|
||
- get the item | ||
```sql | ||
SELECT * | ||
FROM item_item | ||
WHERE id = %s | ||
``` | ||
|
||
- get the user who created the item (the seller) | ||
```sql | ||
SELECT users.username | ||
FROM auth_user AS users, item_item AS items | ||
WHERE items.id = %s AND items.created_by_id = users.id | ||
``` | ||
|
||
- get all non sold items that are in the same category with the item | ||
```sql | ||
SELECT * | ||
FROM item_item | ||
WHERE category_id = %s AND is_sold = False AND id != %s | ||
``` | ||
|
||
## new | ||
|
||
- insert new item | ||
```sql | ||
INSERT INTO item_item (category_id, name, description, price, image, created_by_id, is_sold, created_at) | ||
VALUES (%s, %s, %s, %s, %s, %s, False, CURRENT_TIMESTAMP) | ||
``` | ||
|
||
## Update | ||
- update the item with new information | ||
```sql | ||
UPDATE item_item | ||
SET name = %s, | ||
description = %s, | ||
price = %s, | ||
image = %s, | ||
is_sold = %s | ||
WHERE id = %s; | ||
``` | ||
|
||
## Delete | ||
|
||
- delete the item | ||
```sql | ||
DELETE FROM item_item | ||
WHERE id = %s | ||
``` |