您所在的位置:首页 - 百科 - 正文百科

电商说说语录经典

李娇
李娇 2024-05-25 【百科】 415人已围观

摘要```sql--创建用户表CREATETABLEusers(user_idINTPRIMARYKEY,usernameVARCHAR(50),emailVARCHAR(100),ageINT,date

```sql

创建用户表

CREATE TABLE users (

user_id INT PRIMARY KEY,

username VARCHAR(50),

email VARCHAR(100),

age INT,

date_registered DATE

);

创建订单表

CREATE TABLE orders (

order_id INT PRIMARY KEY,

user_id INT,

order_date DATE,

total_amount DECIMAL(10, 2),

FOREIGN KEY (user_id) REFERENCES users(user_id)

);

创建产品表

CREATE TABLE products (

product_id INT PRIMARY KEY,

product_name VARCHAR(100),

price DECIMAL(10, 2),

category VARCHAR(50),

stock_quantity INT

);

创建订单详情表

CREATE TABLE order_details (

order_id INT,

product_id INT,

quantity INT,

PRIMARY KEY (order_id, product_id),

FOREIGN KEY (order_id) REFERENCES orders(order_id),

FOREIGN KEY (product_id) REFERENCES products(product_id)

);

查询用户及其订单数量

SELECT

u.username,

COUNT(o.order_id) AS order_count

FROM users u

LEFT JOIN orders o ON u.user_id = o.user_id

GROUP BY u.username;

查询每个商品的销售总额及销售数量

SELECT

p.product_name,

SUM(od.quantity) AS total_quantity,

SUM(od.quantity * p.price) AS total_sales_amount

FROM products p

LEFT JOIN order_details od ON p.product_id = od.product_id

GROUP BY p.product_name;

```

https://ksdln.com/

Tags: 电商说说语录经典 电商句子 电商常用语 电商短语 电商语言

最近发表

icp沪ICP备2023034348号-27
取消
微信二维码
支付宝二维码

目录[+]