MySQL简单查询详解-单表查询
MySQL简单查询详解-单表查询
作者:尹正杰
版权声明:原创作品,谢绝转载!否则将追究法律责任。

mysql> create table stars(SID int unsigned auto_increment not null unique key,Name char() not null,Age tinyint unsigned not null,Gender Enum('boy','girl') not null, Tearch char());
Query OK, rows affected (0.01 sec) mysql>
mysql> desc stars;
+--------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+---------------------+------+-----+---------+----------------+
| SID | int() unsigned | NO | PRI | NULL | auto_increment |
| Name | char() | NO | | NULL | |
| Age | tinyint() unsigned | NO | | NULL | |
| Gender | enum('boy','girl') | NO | | NULL | |
| Tearch | char() | YES | | NULL | |
+--------+---------------------+------+-----+---------+----------------+
rows in set (0.00 sec) mysql>
mysql> insert into stars values (,"sun wukong",,'boy','putilaozu'),(,'yinzhengjie',,'boy','himslef'),(,'liufei',,'boy','jia baoyu');
Query OK, rows affected (0.00 sec)
Records: Duplicates: Warnings: mysql>
mysql> insert into stars values (,"han senyu",,'boy','cang laoshi'),(,'jia shanpeng',,'boy','ji laosi'),(,'wu zhiguang',,'boy','ma laoshi');
Query OK, rows affected (0.00 sec)
Records: Duplicates: Warnings: mysql>
mysql> select * from stars;
+-----+--------------+-----+--------+-------------+
| SID | Name | Age | Gender | Tearch |
+-----+--------------+-----+--------+-------------+
| | sun wukong | | boy | putilaozu |
| | yinzhengjie | | boy | himslef |
| | liufei | | boy | jia baoyu |
| | han senyu | | boy | cang laoshi |
| | jia shanpeng | | boy | ji laosi |
| | wu zhiguang | | boy | ma laoshi |
+-----+--------------+-----+--------+-------------+
rows in set (0.00 sec)
mysql> select * from stars where Age between and ;
+-----+--------------+-----+--------+-------------+
| SID | Name | Age | Gender | Tearch |
+-----+--------------+-----+--------+-------------+
| | yinzhengjie | | boy | himslef |
| | han senyu | | boy | cang laoshi |
| | jia shanpeng | | boy | ji laosi |
+-----+--------------+-----+--------+-------------+
rows in set (0.00 sec) mysql>
mysql> select Name,Age from stars where Age in (,);
+-------------+-----+
| Name | Age |
+-------------+-----+
| yinzhengjie | |
| liufei | |
| wu zhiguang | |
+-------------+-----+
rows in set (0.00 sec) mysql>
mysql> select Name,Age from stars where Name rlike '^y.*';
+-------------+-----+
| Name | Age |
+-------------+-----+
| yinzhengjie | |
+-------------+-----+
row in set (0.00 sec) mysql>
mysql> insert into stars values(,'jenny',,'girl','Li ming'),(,'danny',,'boy',NULL);
Query OK, rows affected (0.00 sec)
Records: Duplicates: Warnings: mysql> select * from stars;
+-----+--------------+-----+--------+-------------+
| SID | Name | Age | Gender | Tearch |
+-----+--------------+-----+--------+-------------+
| | sun wukong | | boy | putilaozu |
| | yinzhengjie | | boy | himslef |
| | liufei | | boy | jia baoyu |
| | han senyu | | boy | cang laoshi |
| | jia shanpeng | | boy | ji laosi |
| | wu zhiguang | | boy | ma laoshi |
| | jenny | | girl | Li ming |
| | danny | | boy | NULL |
+-----+--------------+-----+--------+-------------+
rows in set (0.00 sec) mysql>
mysql> select Name,Tearch from stars where Tearch is NULL;
+-------+--------+
| Name | Tearch |
+-------+--------+
| danny | NULL |
+-------+--------+
row in set (0.00 sec) mysql> select Name,Tearch from stars where Tearch is NOT NULL;
+--------------+-------------+
| Name | Tearch |
+--------------+-------------+
| sun wukong | putilaozu |
| yinzhengjie | himslef |
| liufei | jia baoyu |
| han senyu | cang laoshi |
| jia shanpeng | ji laosi |
| wu zhiguang | ma laoshi |
| jenny | Li ming |
+--------------+-------------+
rows in set (0.00 sec) mysql>
mysql> select database();
+-------------+
| database() |
+-------------+
| yinzhengjie |
+-------------+
row in set (0.00 sec) mysql> show tables;
+-----------------------+
| Tables_in_yinzhengjie |
+-----------------------+
| stars |
| t1 |
+-----------------------+
rows in set (0.01 sec) mysql>
mysql> select * from stars;
+-----+--------------+-----+--------+-------------+
| SID | Name | Age | Gender | Tearch |
+-----+--------------+-----+--------+-------------+
| | sun wukong | | boy | putilaozu |
| | yinzhengjie | | boy | himslef |
| | liufei | | boy | jia baoyu |
| | han senyu | | boy | cang laoshi |
| | jia shanpeng | | boy | ji laosi |
| | wu zhiguang | | boy | ma laoshi |
| | jenny | | girl | Li ming |
| | danny | | boy | NULL |
+-----+--------------+-----+--------+-------------+
rows in set (0.00 sec) mysql> select Name,Age from stars where Age > and Gender = 'boy';
+-------------+-----+
| Name | Age |
+-------------+-----+
| sun wukong | |
| yinzhengjie | |
| liufei | |
| han senyu | |
| wu zhiguang | |
+-------------+-----+
rows in set (0.00 sec) mysql>
mysql> select * from stars;
+-----+--------------+-----+--------+-------------+
| SID | Name | Age | Gender | Tearch |
+-----+--------------+-----+--------+-------------+
| | sun wukong | | boy | putilaozu |
| | yinzhengjie | | boy | himslef |
| | liufei | | boy | jia baoyu |
| | han senyu | | boy | cang laoshi |
| | jia shanpeng | | boy | ji laosi |
| | wu zhiguang | | boy | ma laoshi |
| | jenny | | girl | Li ming |
| | danny | | boy | NULL |
+-----+--------------+-----+--------+-------------+
rows in set (0.00 sec) mysql>
mysql> select Name,Age from stars where Age > and Gender = 'boy' order by Name desc;
+-------------+-----+
| Name | Age |
+-------------+-----+
| yinzhengjie | |
| wu zhiguang | |
| sun wukong | |
| liufei | |
| han senyu | |
+-------------+-----+
rows in set (0.00 sec) mysql> select Name,Age from stars where Age > and Gender = 'boy' order by Name asc;
+-------------+-----+
| Name | Age |
+-------------+-----+
| han senyu | |
| liufei | |
| sun wukong | |
| wu zhiguang | |
| yinzhengjie | |
+-------------+-----+
rows in set (0.00 sec) mysql>
mysql> select * from stars;
+-----+--------------+-----+--------+-------------+
| SID | Name | Age | Gender | Tearch |
+-----+--------------+-----+--------+-------------+
| | sun wukong | | boy | putilaozu |
| | yinzhengjie | | boy | himslef |
| | liufei | | boy | jia baoyu |
| | han senyu | | boy | cang laoshi |
| | jia shanpeng | | boy | ji laosi |
| | wu zhiguang | | boy | ma laoshi |
| | jenny | | girl | Li ming |
| | danny | | boy | NULL |
+-----+--------------+-----+--------+-------------+
rows in set (0.00 sec) mysql> select SUM(Age) from stars;
+----------+
| SUM(Age) |
+----------+
| |
+----------+
row in set (0.00 sec) mysql> select MAX(Age) from stars;
+----------+
| MAX(Age) |
+----------+
| |
+----------+
row in set (0.00 sec) mysql> select MIN(Age) from stars;
+----------+
| MIN(Age) |
+----------+
| |
+----------+
row in set (0.00 sec) mysql> select COUNT(Age) from stars;
+------------+
| COUNT(Age) |
+------------+
| |
+------------+
row in set (0.00 sec) mysql> select COUNT(*) from stars; #这种用法不建议,“*”的效率是非常低的,不如加入一个字段进去!
+----------+
| COUNT(*) |
+----------+
| |
+----------+
row in set (0.00 sec) mysql>
mysql> select SUM(Age) from stars where Age > ;
+----------+
| SUM(Age) |
+----------+
| |
+----------+
row in set (0.00 sec) mysql>
mysql> select * from stars;
+-----+--------------+-----+--------+-------------+
| SID | Name | Age | Gender | Tearch |
+-----+--------------+-----+--------+-------------+
| | sun wukong | | boy | putilaozu |
| | yinzhengjie | | boy | himslef |
| | liufei | | boy | jia baoyu |
| | han senyu | | boy | cang laoshi |
| | jia shanpeng | | boy | ji laosi |
| | wu zhiguang | | boy | ma laoshi |
| | jenny | | girl | Li ming |
| | danny | | boy | NULL |
+-----+--------------+-----+--------+-------------+
rows in set (0.00 sec) mysql>
mysql>
mysql> select Gender,SUM(Age) from stars group by Gender;
+--------+----------+
| Gender | SUM(Age) |
+--------+----------+
| boy | |
| girl | |
+--------+----------+
rows in set (0.00 sec) mysql>
mysql> select * from stars;
+-----+--------------+-----+--------+-------------+
| SID | Name | Age | Gender | Tearch |
+-----+--------------+-----+--------+-------------+
| | sun wukong | | boy | putilaozu |
| | yinzhengjie | | boy | himslef |
| | liufei | | boy | jia baoyu |
| | han senyu | | boy | cang laoshi |
| | jia shanpeng | | boy | ji laosi |
| | wu zhiguang | | boy | ma laoshi |
| | jenny | | girl | Li ming |
| | danny | | boy | NULL |
+-----+--------------+-----+--------+-------------+
rows in set (0.00 sec) mysql>
mysql> alter table stars add ClassID tinyint unsigned;
Query OK, rows affected (0.00 sec)
Records: Duplicates: Warnings: mysql>
mysql> select * from stars;
+-----+--------------+-----+--------+-------------+---------+
| SID | Name | Age | Gender | Tearch | ClassID |
+-----+--------------+-----+--------+-------------+---------+
| | sun wukong | | boy | putilaozu | NULL |
| | yinzhengjie | | boy | himslef | NULL |
| | liufei | | boy | jia baoyu | NULL |
| | han senyu | | boy | cang laoshi | NULL |
| | jia shanpeng | | boy | ji laosi | NULL |
| | wu zhiguang | | boy | ma laoshi | NULL |
| | jenny | | girl | Li ming | NULL |
| | danny | | boy | NULL | NULL |
+-----+--------------+-----+--------+-------------+---------+
rows in set (0.00 sec) mysql> update stars set ClassID = where SID = ;
Query OK, row affected (0.00 sec)
Rows matched: Changed: Warnings: mysql> update stars set ClassID = where SID = ;
Query OK, row affected (0.00 sec)
Rows matched: Changed: Warnings: mysql> update stars set ClassID = where SID = ;
Query OK, row affected (0.00 sec)
Rows matched: Changed: Warnings: mysql> update stars set ClassID = where SID = ;
Query OK, row affected (0.01 sec)
Rows matched: Changed: Warnings: mysql> update stars set ClassID = where SID = ;
Query OK, row affected (0.00 sec)
Rows matched: Changed: Warnings: mysql> update stars set ClassID = where SID = ;
Query OK, row affected (0.00 sec)
Rows matched: Changed: Warnings: mysql> update stars set ClassID = where SID = ;
Query OK, row affected (0.00 sec)
Rows matched: Changed: Warnings: mysql> update stars set ClassID = where SID = ;
Query OK, row affected (0.00 sec)
Rows matched: Changed: Warnings: mysql>
mysql> select * from stars;
+-----+--------------+-----+--------+-------------+---------+
| SID | Name | Age | Gender | Tearch | ClassID |
+-----+--------------+-----+--------+-------------+---------+
| | sun wukong | | boy | putilaozu | |
| | yinzhengjie | | boy | himslef | |
| | liufei | | boy | jia baoyu | |
| | han senyu | | boy | cang laoshi | |
| | jia shanpeng | | boy | ji laosi | |
| | wu zhiguang | | boy | ma laoshi | |
| | jenny | | girl | Li ming | |
| | danny | | boy | NULL | |
+-----+--------------+-----+--------+-------------+---------+
rows in set (0.00 sec) mysql>
mysql> select ClassID,Count(Name),sum(Age) from stars group by ClassID;
+---------+-------------+----------+
| ClassID | Count(Name) | sum(Age) |
+---------+-------------+----------+
| | | |
| | | |
| | | |
| | | |
| | | |
+---------+-------------+----------+
rows in set (0.00 sec) mysql>
mysql> select ClassID,Count(Name) from stars group by ClassID having Count(Name) >=;
+---------+-------------+
| ClassID | Count(Name) |
+---------+-------------+
| | |
| | |
+---------+-------------+
rows in set (0.00 sec) mysql>
mysql> select ClassID from stars group by ClassID having sum(Age) >=;
+---------+
| ClassID |
+---------+
| |
+---------+
row in set (0.00 sec) mysql>
mysql> select * from stars ;
+-----+--------------+-----+--------+-------------+---------+
| SID | Name | Age | Gender | Tearch | ClassID |
+-----+--------------+-----+--------+-------------+---------+
| | sun wukong | | boy | putilaozu | |
| | yinzhengjie | | boy | himslef | |
| | liufei | | boy | jia baoyu | |
| | han senyu | | boy | cang laoshi | |
| | jia shanpeng | | boy | ji laosi | |
| | wu zhiguang | | boy | ma laoshi | |
| | jenny | | girl | Li ming | |
| | danny | | boy | NULL | |
+-----+--------------+-----+--------+-------------+---------+
rows in set (0.00 sec) mysql>
mysql> select * from stars limit ;
+-----+-------------+-----+--------+-----------+---------+
| SID | Name | Age | Gender | Tearch | ClassID |
+-----+-------------+-----+--------+-----------+---------+
| | sun wukong | | boy | putilaozu | |
| | yinzhengjie | | boy | himslef | |
+-----+-------------+-----+--------+-----------+---------+
rows in set (0.00 sec) mysql> select * from stars limit ,;
+-----+--------------+-----+--------+-------------+---------+
| SID | Name | Age | Gender | Tearch | ClassID |
+-----+--------------+-----+--------+-------------+---------+
| | liufei | | boy | jia baoyu | |
| | han senyu | | boy | cang laoshi | |
| | jia shanpeng | | boy | ji laosi | |
+-----+--------------+-----+--------+-------------+---------+
rows in set (0.00 sec) mysql>

MySQL简单查询详解-单表查询的更多相关文章
- mysql查询操作之单表查询、多表查询、子查询
一.单表查询 单表查询的完整语法: .完整语法(语法级别关键字的排列顺序如下) select distinct 字段1,字段2,字段3,... from 库名.表名 where 约束条件 group ...
- MySQL数据库实验二:单表查询
实验二 单表查询 一.实验目的 理解SELECT语句的操作和基本使用方法. 二.实验环境 是MS SQL SERVER 2005的中文客户端. 三.实验示例 1.查询全体学生的姓名.学号.所在系. ...
- 不使用left-join等多表关联查询,只用单表查询和Java程序,简便实现“多表查询”效果
上次我们提到,不使用left-loin关联查询,可能是为了提高效率或者配置缓存,也可以简化一下sql语句的编写.只写单表查询,sql真得太简单了.问题是,查询多个表的数据还是非常需要的. 因此,存在这 ...
- Hibernate第十篇【Hibernate查询详解、分页查询】
前言 在Hibernate的第二篇中只是简单地说了Hibernate的几种查询方式-.到目前为止,我们都是使用一些简单的主键查询阿-使用HQL查询所有的数据-.本博文主要讲解Hibernate的查询操 ...
- MySQL常用查询命令(单表查询)
查询语法如下: select... from... where... group by... (having)... order by...; 顺序是from (从指定表中) where (具体条件) ...
- MySql语句常用命令整理---单表查询
初始化t_employee表 创建t_employee表 -- DROP TABLE IF EXISTS test; CREATE TABLE t_employee ( _id INTEGER PRI ...
- MySQL(九)之数据表的查询详解(SELECT语法)一
这一篇是MySQL中的重点也是相对于MySQL中比较难得地方,个人觉得要好好的去归类,并多去练一下题目.MySQL的查询也是在笔试中必有的题目.希望我的这篇博客能帮助到大家! 重感冒下的我,很难受!k ...
- mysql数据库之单表查询
单标查询 单表查询语句 关键字执行的优先级 简单查询 where约束 group by 聚合函数 HAVING过滤 order by 查询排序 LIMIT限制查询的记录数 使用正则表达式查询 单表查询 ...
- MySql(六)单表查询
十.单表查询 一.单表查询的语法 SELECT 字段1,字段2... FROM 表名 WHERE 条件 GROUP BY field HAVING 筛选 ORDER BY field LIMIT 限制 ...
随机推荐
- JAVA eclipse Maven项目红叹号解决方案
我是通过 Windows --> show view --> problems 查看到发现 ch.qos.logback 1.1.1 出现了错误, 于是我换成了 ch.qos.logbac ...
- C#易忘点
下面是自己总结的一些C#语言方面用的少容易忘的地方,总结一下,在用的时候看一下.(仅针对本人) 参数数组 定义一个函数,用来取得数字的和,但是数字的个数不确定. 解决方案: 1,定义一个函数,参数传递 ...
- 关于OBS获取显示器黑屏的解决办法
近来看到许多人说OBS获取显示器源的时候黑屏,下面介绍下相关处理办法. 第一种,先尝试把OBS程序的兼容性设置成Win 7和管理员身份,具体操作: 设置成这样,如果能够获取到显示器,那么问题解决,否则 ...
- mui框架(一)
1.界面初始化 初始化就是把一切程序设为默认状态,把没准备的准备好. mui框架将很多功能配置都集中在mui.init方法中,要使用某项功能,只需要在mui.init方法中完成对应参数配置即可,目前支 ...
- 20135202闫佳歆--week7 可执行程序的装载--学习笔记
此为个人学习笔记存档 week 7 可执行程序的装载 一.预处理.编译.链接和目标文件的格式 可执行文件的创建--预处理.编译和链接 cd Code vi hello.c gcc -E -o hell ...
- 2017-2018 第一学期201623班《程序设计与数据结构》-第5&6周作业问题总结
一.作业内容 第5周作业 http://www.cnblogs.com/rocedu/p/7484252.html#WEEK05 第6周作业 http://www.cnblogs.com/rocedu ...
- 第二个Sprint冲刺第四天(燃尽图)
- What is the difference between WinRT, UWP and WPF?
在学习UWP的过程中确实有这个迷惑,在此分享一下. UWP (Universal Windows platform), Metro and WinRT are all result of Micros ...
- idea不能跟随输入法问题
在写注释的时候会发现输入法不跟随,这是idea工具本身存在的bug,这个问题很头疼,我找了好多办法都不行,比如删除idea自带的jre,这个办法对我的2018.1.5版本并不适用,以下办法是不需要删除 ...
- Java时间的转换
String DATE_FORMAT = "yyyy-MM-dd"; LocalDate localDate = LocalDate.now(); long startTime = ...