Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- docker run -it -e POSTGRES_USER=username -e POSTGRES_PASSWORD=password postgres:11.2-alpine
- docker container ls
- docker exec -it b77fe8a3722d bash
- psql -U username
- CREATE DATABASE ecommerce;
- CREATE TABLE users (id SERIAL, name TEXT);
- CREATE TABLE adresses (id SERIAL, numero INTEGER, street TEXT, postal_code INTEGER, town TEXT, user_id INTEGER);
- CREATE TABLE products (id SERIAL, name TEXT, description TEXT);
- CREATE TABLE Orders (id SERIAL, order_date TIMESTAMP, amount INTEGER, user_id INTEGER);
- CREATE TABLE order_products (order_id INTEGER, product_id INTEGER);
- INSERT INTO users (name, genre) VALUES ('Adeline', 'F');
- INSERT INTO adresses (numero, street, postal_code, town, user_id) VALUES (3, 'rue Napoleon', 75001, 'Paris', 1);
- INSERT INTO products (name, description) VALUES ('Pull', 'Pull gris'), ('Tshirt', 'Tshirt blanc'), ('Jean', 'Jean brut'), ('Chaussettes', 'Chaussettes noires'), ('Pantalon', 'Pantalon noir'), ('Jupe', 'Jupe bleue');
- INSERT INTO orders (order_date, amount, user_id) VALUES ('01/18/2019', 58, 1);
- INSERT INTO order_products (order_id, product_id) VALUES (1, 2), (1,4), (1,3);
- - Donnez les users qui ont des commandes
- SELECT (name) FROM users u INNER JOIN orders o ON u.id = o.user_id GROUP BY (name);
- - Donnez les users qui n'ont pas de commandes
- SELECT (name) FROM users u LEFT JOIN orders o ON u.id = o.user_id WHERE o.user_id IS NULL GROUP BY (name);
- - Donnez les adresses des users qui ont commandé
- SELECT a.* FROM adresses a INNER JOIN orders o ON a.user_id = o.user_id;
- - Donnez le nombre de commande par mois
- select date_part('month', order_date) as month, count(id) from orders GROUP BY (month);
- - Donnez la moyenne des montants de commandes
- select avg(amount) from orders;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement