Skip to content

Commit

Permalink
docs: add the sql queries we used in this project into an md file
Browse files Browse the repository at this point in the history
  • Loading branch information
bouzenaali committed Jan 24, 2024
1 parent b7a5206 commit 56a0043
Showing 1 changed file with 93 additions and 0 deletions.
93 changes: 93 additions & 0 deletions sql.md
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
```

0 comments on commit 56a0043

Please sign in to comment.