-- 查询所有记录
SELECT * FROM your_table;
-- 查询特定条件的记录
SELECT * FROM your_table WHERE condition;
-- 插入一条记录
INSERT INTO your_table (column1, column2, column3) VALUES (value1, value2, value3);
-- 更新记录
UPDATE your_table SET column1 = new_value1, column2 = new_value2 WHERE condition;
UPDATE users SET age = 26 WHERE name = 'John Doe';
-- 删除记录
DELETE FROM your_table WHERE condition;
DELETE FROM users WHERE name = 'John Doe';
用于返回两个表中满足连接条件的行。如果两个表中没有匹配的行,则不会返回。
SELECT orders.order_id, customers.customer_name
FROM orders
INNER JOIN customers ON orders.customer_id = customers.customer_id;
LEFT JOIN 返回左表中的所有行,以及与右表中匹配的行。如果右表中没有匹配的行,则结果中右表的列将包含 NULL 值。
SELECT customers.customer_id, customers.customer_name, orders.order_id
FROM customers
LEFT JOIN orders ON customers.customer_id = orders.customer_id;
RIGHT JOIN 返回右表中的所有行,以及与左表中匹配的行。如果左表中没有匹配的行,则结果中左表的列将包含 NULL 值。
SELECT column1, column2, ...
FROM table1
RIGHT JOIN table2 ON table1.column_name = table2.column_name;
FULL JOIN 返回两个表中的所有行,如果没有匹配的行,结果集中对应位置的列将包含 NULL 值。
SELECT column1, column2, ...
FROM table1
FULL JOIN table2 ON table1.column_name = table2.column_name;
CROSS JOIN 返回两个表的笛卡尔积,即两个表的所有行组合。
SELECT column1, column2, ...
FROM table1
CROSS JOIN table2;
CREATE TABLE users (
user_id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50) NOT NULL,
email VARCHAR(100) NOT NULL,
birthdate DATE,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);