MySQL常用操作、查询与函数

MySQL环境下

#使用st数据库
Mysql>use st
#查询student表里的数据,显示所有字段的数据
mysql> select * from student;
±----------±-------±-----±-----±------+
| Sno | Sname | Ssex | Sage | Sdept |
±----------±-------±-----±-----±------+
| 201215121 | 李勇 | 男 | 20 | CS |
| 201215122 | 刘晨 | 女 | 19 | CS |
| 201215123 | 王小敏 | 女 | 18 | MA |
| 201215124 | 张立 | 男 | 19 | IS |
| 201215125 | 王敏 | 女 | 18 | A |
±----------±-------±-----±-----±------+
5 rows in set (0.00 sec)

#查询student表中的数据,只显示name字段的数据
mysql> select name from student;
ERROR 1054 (42S22): Unknown column ‘name’ in ‘field list’
mysql> select Sname from student;
±-------+
| Sname |
±-------+
| 李勇 |
| 刘晨 |
| 王敏 |
| 王小敏 |
| 张立 |
±-------+
5 rows in set (0.00 sec)

#按条件进行查询,查询年龄大于19的数据
mysql> select * from student where age >19;
ERROR 1054 (42S22): Unknown column ‘age’ in ‘where clause’
mysql> select * from student where Sage >19;
±----------±------±-----±-----±------+
| Sno | Sname | Ssex | Sage | Sdept |
±----------±------±-----±-----±------+
| 201215121 | 李勇 | 男 | 20 | CS |
±----------±------±-----±-----±------+
1 row in set (0.00 sec)

#查询总共有多少条数据
mysql> select count() from student;
±---------+
| count(
) |
±---------+
| 5 |
±---------+
1 row in set (0.00 sec)

#查询年龄总和
mysql> select sum(age) from user;
ERROR 1146 (42S02): Table ‘st.user’ doesn’t exist
mysql> select sum(Sage) from user;
ERROR 1146 (42S02): Table ‘st.user’ doesn’t exist
mysql> select sum(Sage) from student;
±----------+
| sum(Sage) |
±----------+
| 94 |
±----------+
1 row in set (0.00 sec)

#查询年龄最大的数据
mysql> select max(Sage) from student;
±----------+
| max(Sage) |
±----------+
| 20 |
±----------+
1 row in set (0.00 sec)

#按性别进行分组查询
mysql> select * from student group by Ssex;
±----------±------±-----±-----±------+
| Sno | Sname | Ssex | Sage | Sdept |
±----------±------±-----±-----±------+
| 201215122 | 刘晨 | 女 | 19 | CS |
| 201215121 | 李勇 | 男 | 20 | CS |
±----------±------±-----±-----±------+
2 rows in set (0.01 sec)

#按年龄降序排列查询
mysql> select * from student order by Sage desc;
±----------±-------±-----±-----±------+
| Sno | Sname | Ssex | Sage | Sdept |
±----------±-------±-----±-----±------+
| 201215121 | 李勇 | 男 | 20 | CS |
| 201215122 | 刘晨 | 女 | 19 | CS |
| 201215124 | 张立 | 男 | 19 | IS |
| 201215123 | 王小敏 | 女 | 18 | MA |
| 201215125 | 王敏 | 女 | 18 | A |
±----------±-------±-----±-----±------+
5 rows in set (0.00 sec)

#按年龄升序排列查询
mysql> select * from student ordey by Sage asc;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘by Sage asc’ at line 1
mysql> select * from student order by Sage asc;
±----------±-------±-----±-----±------+
| Sno | Sname | Ssex | Sage | Sdept |
±----------±-------±-----±-----±------+
| 201215123 | 王小敏 | 女 | 18 | MA |
| 201215125 | 王敏 | 女 | 18 | A |
| 201215122 | 刘晨 | 女 | 19 | CS |
| 201215124 | 张立 | 男 | 19 | IS |
| 201215121 | 李勇 | 男 | 20 | CS |
±----------±-------±-----±-----±------+
5 rows in set (0.00 sec)

#查询前5条数据
mysql> select * from student limit 0.5;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘0.5’ at line 1
mysql> select * from student limit 0,5;
±----------±-------±-----±-----±------+
| Sno | Sname | Ssex | Sage | Sdept |
±----------±-------±-----±-----±------+
| 201215121 | 李勇 | 男 | 20 | CS |
| 201215122 | 刘晨 | 女 | 19 | CS |
| 201215123 | 王小敏 | 女 | 18 | MA |
| 201215124 | 张立 | 男 | 19 | IS |
| 201215125 | 王敏 | 女 | 18 | A |
±----------±-------±-----±-----±------+
5 rows in set (0.00 sec)

猜你喜欢

转载自blog.csdn.net/weixin_46555054/article/details/121442844
今日推荐