mysql查询语句大全及用法
时间: 2024-12-13 22:02:24 浏览: 3
MySQL查询语句大全包括了许多基本操作,如选择数据、过滤条件、排序结果等。以下是常见的几种SQL语句及其用途:
1. **SELECT** 语句:用于从数据库表中选择数据,语法如下:
```sql
SELECT column_name(s) FROM table_name WHERE condition;
```
示例:`SELECT * FROM users WHERE age > 18;`
2. **INSERT INTO** 语句:用于插入新记录到表中:
```sql
INSERT INTO table_name (column1, column2, ...) VALUES (value1, value2, ...);
```
示例:`INSERT INTO users (name, email) VALUES ('John', 'john@example.com');`
3. **UPDATE** 语句:更新表中已有记录的数据:
```sql
UPDATE table_name SET column = new_value WHERE condition;
```
示例:`UPDATE users SET age = 20 WHERE id = 1;`
4. **DELETE FROM** 语句:删除表中的记录:
```sql
DELETE FROM table_name WHERE condition;
```
示例:`DELETE FROM users WHERE email = 'john@example.com';`
5. **JOIN** 语句:连接两个或多个表:
```sql
SELECT * FROM table1 JOIN table2 ON table1.column = table2.column;
```
示例:`SELECT * FROM users JOIN orders ON users.id = orders.user_id;`
6. **GROUP BY** 用于分组数据并计算聚合函数(如COUNT, SUM, AVG等):
```sql
SELECT column, COUNT(*) FROM table GROUP BY column;
```
7. **ORDER BY** 用于对查询结果进行排序:
```sql
SELECT * FROM table_name ORDER BY column_name ASC|DESC;
```
阅读全文