ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

MySQL常用操作、查询与函数

2021-11-20 18:03:04  阅读:152  来源: 互联网

标签:函数 mysql Sage 查询 ----- ------+ MySQL ---------- select


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)

标签:函数,mysql,Sage,查询,-----,------+,MySQL,----------,select
来源: https://blog.csdn.net/weixin_46555054/article/details/121442844

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有