生成sql订单表与用户和课程表的自连接
时间: 2023-09-24 13:03:50 浏览: 71
订单表:
CREATE TABLE orders (
order_id INT PRIMARY KEY,
user_id INT,
course_id INT,
order_date DATE,
amount DECIMAL(10,2),
FOREIGN KEY (user_id) REFERENCES users(user_id),
FOREIGN KEY (course_id) REFERENCES courses(course_id)
);
用户表:
CREATE TABLE users (
user_id INT PRIMARY KEY,
username VARCHAR(50),
email VARCHAR(50),
password VARCHAR(50)
);
课程表:
CREATE TABLE courses (
course_id INT PRIMARY KEY,
course_name VARCHAR(50),
instructor VARCHAR(50),
price DECIMAL(10,2)
);
自连接:
SELECT o.order_id, u.username, c.course_name
FROM orders o
INNER JOIN users u ON o.user_id = u.user_id
INNER JOIN courses c ON o.course_id = c.course_id;
阅读全文