sql语句大全实例教程
时间: 2023-08-31 07:12:42 浏览: 65
SQL(Structured Query Language)是一种用于管理关系型数据库的标准编程语言。下面是一些常用的 SQL 语句和示例教程:
1. 创建表格
```
CREATE TABLE customers (
id INT PRIMARY KEY,
name VARCHAR(50),
email VARCHAR(50)
);
```
2. 插入数据
```
INSERT INTO customers (id, name, email) VALUES
(1, 'John', 'john@example.com'),
(2, 'Mary', 'mary@example.com'),
(3, 'Bob', 'bob@example.com');
```
3. 查询数据
```
SELECT * FROM customers;
```
4. 更新数据
```
UPDATE customers SET email = 'john@gmail.com' WHERE id = 1;
```
5. 删除数据
```
DELETE FROM customers WHERE id = 3;
```
6. 连接表格
```
SELECT
customers.name,
orders.product_name,
orders.price
FROM
customers
INNER JOIN
orders ON customers.id = orders.customer_id;
```
7. 分组数据
```
SELECT
customers.name,
COUNT(orders.id) AS num_orders,
SUM(orders.price) AS total_spent
FROM
customers
INNER JOIN
orders ON customers.id = orders.customer_id
GROUP BY
customers.name;
```
8. 排序数据
```
SELECT * FROM customers ORDER BY name ASC;
```
9. 聚合数据
```
SELECT AVG(price) AS avg_price FROM orders;
```
以上是一些常用的 SQL 语句和示例教程,希望对你有所帮助!
阅读全文