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 限制 ...
随机推荐
- LOJ #2135. 「ZJOI2015」幻想乡战略游戏
#2135. 「ZJOI2015」幻想乡战略游戏 链接 分析: 动态点分治,求加权重心,带修改. 考虑如果知道了一个点s,如何求答案,那么首先可以点分治的思想,求每个联通块内所有点到分治中心距离和,然 ...
- C++学习之从C到C++
头文件的包含 包含头文件可以不加.h结尾,如iostream,一些常用的头文件在引用时可以不加.h后缀,并在开头增加c,如: #include <cstdio> #include < ...
- VS相关
快速显示函数名称 ctrl+alt+T 显示函数参数说明 ctrl+shift+space,光标放在函数里面 link 1123错误,vs2010的问题. 点击工程-属性-ManifestTool-I ...
- Flask学习-前言
前言 使用Flask断断续续加起来快一年了,但是一直没有从源码层去了解其实现原理.加上自己python基础不扎实,所以准备从看一个开源项目开始,希望能够从中窥得武功精髓,让自己水平更上一层楼. Fla ...
- resource fork, Finder information, or similar detritus not allowed
1.关闭当前项目和Xcode 2.打开终端或者iterm cd ~/Library/Developer/Xcode/DerivedData/ 3. xattr -rc . 4.重新打开项目 5.如果不 ...
- jenkins pipeline 部署
一.git 版本控制结合jenkins 发布 sh-4.2$ git branch sh-4.2$ git chekout master sh-4.2$ git tag v1.1 sh-4.2$ gi ...
- Seay源代码审计系统的配置和安装
2014年7月31日 Seay源代码审计系统2.1 时隔刚好一年之久,源代码审计系统再次更新,这次主要优化审计体验,优化了漏洞规则,算是小幅更新,原来使用者打开程序会提示自动更新. 1.优化原有规则, ...
- M1阶段事后总结
设想和目标 1. 我们的软件要解决什么问题?是否定义得很清楚?是否对典型用户和典型场景有清晰的描述?我们组要爬取网上的内容供下一组使用,定义的不太清楚,因为用户只有下一个团队所以没有进行详细的需求分析 ...
- 【Alpha】功能规格说明书
更新说明:从用户需求分析中剥离有关用户场景分析部分,加入功能规格说明书. Github地址:https://github.com/buaase/Phylab-Web/blob/master/docs/ ...
- VS2013安装及测试
一.Visual Studio的安装 首先是Visual Studio英文版的安装,安装完成后,为了用的时候方便,我从官网下载Visual Studio 2013的语言包并安装. 二.进行单元测试. ...