mysql语句2-单表查询
mysql 查询以及多表查询
以下所有表格样例都采用下边这个表格
mysql> select * from benet;
+------+------+----------+
| id | name | nianling |
+------+------+----------+
| 3 | a | 16 |
| 4 | b | 17 |
| 3 | a | 16 |
| 5 | b | 15 |
| 3 | b | 15 |
| 5 | b | 15 |
| 3 | b | 15 |
+------+------+----------+
1. 查询所有的内容。
select * from 表名称;
mysql> select * from benet;
2. 查询某个字段的内容
select 字段名1,字段名2,... from 表名称
mysql> select id from benet;
3.根据条件查询
例子1:
mysql> select * from benet where id=5;
+------+------+----------+
| id | name | nianling |
+------+------+----------+
| 5 | b | 15 |
| 5 | b | 15 |
+------+------+----------+
例子2 查询名字等于a的所有字段
mysql> select * from benet where name='a'; 注意:当条件内容是字符的时候,需要用引号引起来,但是数字不用。
+------+------+----------+
| id | name | nianling |
+------+------+----------+
| 3 | a | 16 |
| 3 | a | 16 |
+------+------+----------+
例子3:名字为b并且id大于3的内容
在多条件查询中 && 符号可以用and代替
mysql> select * from benet where name='b' && id > 3;
+------+------+----------+
| id | name | nianling |
+------+------+----------+
| 4 | b | 17 |
| 5 | b | 15 |
| 5 | b | 15 |
+------+------+----------+
例子4:名字为b并且id大于3并且年龄大于15
mysql> select * from benet where name='b' && id > 3 && nianling > 15;
+------+------+----------+
| id | name | nianling |
+------+------+----------+
| 4 | b | 17 |
+------+------+----------+
4.查询特定的参数
(1).disdinct参数:重复的结果只显示一次
例子:
mysql> select * from benet;
+------+------+----------+
| id | name | nianling |
+------+------+----------+
| 3 | a | 16 |
| 4 | b | 17 |
| 3 | a | 16 |
| 5 | b | 15 |
| 3 | b | 15 |
| 5 | b | 15 |
| 3 | b | 15 |
+------+------+----------+
我们查询id字段
mysql> select id from benet;
+------+
| id |
+------+
| 3 |
| 4 |
| 3 |
| 5 |
| 3 |
| 5 |
| 3 |
+------+
查询后的结果有些是重读的,我们可以让这些重复的结果只显示一次
mysql> select distinct id from benet;
+------+
| id |
+------+
| 3 |
| 4 |
| 5 |
+------+
(2). 在某个范围之间
格式:between....and....
例子:
use benet
create table benet (id int(3),name char(10),nianling int(3));
insert into benet values (1,'a',10), (2,'a',10), (3,'a',10), (3,'b',12),( 3,'b',14), (3,'b',15), (3,'b',16) ,(3,'c',18);
例子:
mysql> select * from benet where nianling between 12 and 16;
+------+------+----------+
| id | name | nianling |
+------+------+----------+
| 3 | b | 12 |
| 3 | b | 14 |
| 3 | b | 15 |
| 3 | b | 16 |
+------+------+----------+
(3)like '关键字' 根据关键字查找
% :表示任意长度的任意字符
_ : 表示单个字符
mysql> create table meinv (name char(30),nianling int(3));
insert into meinv values ('yangmi',25);
insert into meinv values ('gaoyuanyuan',25);
insert into meinv values ('yangziqiong',28);
查找y开头的名字
mysql> select * from meinv where name like 'y%';
+-------------+----------+
| name | nianling |
+-------------+----------+
| yangmi | 25 |
| yangziqiong | 28 |
+-------------+----------+
查找包含yuan的名字
mysql> select * from meinv where name like '%yuan%';
+-------------+----------+
| name | nianling |
+-------------+----------+
| gaoyuanyuan | 25 |
+-------------+----------+
查询某个字段为空值的数据。
mysql> create table abc (name char(20),nianling int(3));
mysql> insert into abc values ('a',10),('b',3),('c',null);
mysql> select * from abc;
+------+----------+
| name | nianling |
+------+----------+
| a | 10 |
| b | 3 |
| c | NULL |
+------+----------+
mysql> select * from abc where nianling is null;
查询年龄字段中为空的数据。
mysql> select * from abc where nianling is not null;
查询年龄字段中不为空的数据。
查询排序
order by 排序的一依据字段
例子1:根据nianling字段内容按照升序排列。
select * from abc order by nianling;
+------+----------+
| name | nianling |
+------+----------+
| c | NULL |
| b | 3 |
| a | 10 |
+------+----------+
例子2:根据nianling字段内容按照降序排列。
mysql> select * from abc order by nianling desc;
+------+----------+
| name | nianling |
+------+----------+
| a | 10 |
| b | 3 |
| c | NULL |
+------+----------+
显示查询后的部分结果limit
例子1:
mysql> select * from abc limit 2;
只显示结果的前两行
+------+----------+
| name | nianling |
+------+----------+
| a | 10 |
| b | 3 |
+------+----------+
聚合计算
依然采用上面的表作为例子
mysql> select sum(nianling) from abc; 求nianling字段所有数据的和
+---------------+
| sum(nianling) |
+---------------+
| 13 |
+---------------+
mysql> select max(nianling) from abc; 求nianling字段所有数据最大值
mysql> select min(nianling) from abc; 求nianling字段所有数据最少值
mysql> select avg(nianling) from abc; 求nianling字段所有数据的平均值
+---------------+
| avg(nianling) |
+---------------+
| 6.5000 |
+---------------+
mysql语句2-单表查询的更多相关文章
- mysql 基础入门 单表查询
单表查询 select 表头,表头 as 别名 ,表头(+-*/的运算) from table_a 1.条件查询 where + 条件 <> , != 不等于 = 等于,也可以表示字符串值 ...
- mysql 数据操作 单表查询 目录
mysql 数据操作 单表查询 mysql 数据操作 单表查询 简单查询 避免重复DISTINCT mysql 数据操作 单表查询 通过四则运算查询 mysql 数据操作 单表查询 concat()函 ...
- mysql 数据操作 单表查询 where 约束 目录
mysql 数据操作 单表查询 where约束 between and or mysql 数据操作 单表查询 where约束 is null in mysql 数据操作 单表查询 where约束 li ...
- mysql 数据操作 单表查询 group by 分组 目录
mysql 数据操作 单表查询 group by 介绍 mysql 数据操作 单表查询 group by 聚合函数 mysql 数据操作 单表查询 group by 聚合函数 没有group by情况 ...
- MySQL数据库之单表查询中关键字的执行顺序
目录 MySQL数据库之单表查询中关键字的执行顺序 1 语法顺序 2 执行顺序 3 关键字使用语法 MySQL数据库之单表查询中关键字的执行顺序 1 语法顺序 select distinct from ...
- MySQL数据库语法-单表查询练习
MySQL数据库语法-单表查询练习 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 本篇博客主要是对聚合函数和分组的练习. 一.数据表和测试数据准备 /* @author :yinz ...
- MySQL学习9 - 单表查询
一.单表查询的语法 二.关键字的执行优先级(重点) 三.单表查询示例 1.where约束 2.group by分组查询 3.聚合函数 4.HAVING过滤 5.order by查询排序 6.limit ...
- mysql四-1:单表查询
一 单表查询的语法 SELECT 字段1,字段2... FROM 表名 WHERE 条件 GROUP BY field HAVING 筛选 ORDER BY field LIMIT 限制条数 二 关键 ...
- mysql数据库之单表查询多表查询
单表查询 前期表准备 create table emp( id int not null unique auto_increment, name varchar(20) not null, sex e ...
- Python、mysql四-1:单表查询
一 单表查询的语法 SELECT 字段1,字段2... FROM 表名 WHERE 条件 GROUP BY field HAVING 筛选 ORDER BY field LIMIT 限制条数 二 关键 ...
随机推荐
- 时间同步之pxe,cobbler,dhcp
ntpdate 时间同步 同步方法 ntpdate ntp服务器IP 例: ntpdate 192.168.37.11 自动运行同步时间脚本 crontab -e * */1 * * * /usr/s ...
- java客户端的elasticSearch索引库的相关操作
package com.hope.es;import org.elasticsearch.client.transport.TransportClient;import org.elasticsear ...
- Jenkins代码检查
目录 一.静态代码分析 二.规范检查 PMD进行检查 分析器区别 三.持续代码质量检测 Maven与SonarQube集成 Jenkins与SonarQube集成 代码扫描 SonarQube集成p3 ...
- [Java Web 王者归来]读书笔记1
第一章 Java web 开发概述 1 WEB服务器运行时一直在TCP 80(默认端口)监听, 若使用其他端口在url中需要显示标注端口号(例如:8080) 2 WEB服务器:微软的IIS.Apach ...
- AtCoder Beginner Contest 172 题解
AtCoder Beginner Contest 172 题解 目录 AtCoder Beginner Contest 172 题解 A - Calc B - Minor Change C - Tsu ...
- LuoguP7478 【A】StickSuger 题解
Content 给定一个长度为 \(n\) 的仅包含小写字母的字符串 \(s\),请找到一个二元组 \((i,j)\)(\(i<j\))使得在交换字符串 \(s\) 的第 \(i\) 个和第 \ ...
- Java 数据类型:集合接口Collection之Set接口HashSet类;LinkedHashSet;TreeSet 类
Collection 之 Set 实现类: HashSet TreeSet 特点: 无序. 元素不可重复. (如果试图添加一个已经有的元素到一个Set集合中,那么会添失败,add()方法返回false ...
- 开启ipv6支持
CentOS6 开启ipv6模块操作在/etc/sysconfig/modules 目录下创建一个脚本,比如叫做 ipv6.modules,脚本中内容如下:#!/bin/shif [ ! -c /p ...
- 如何获取网管MTU
在本机打开dos窗口,执行: ping -f -l 1472 192.168.0.1 其中192.168.0.1是网关IP地址,1472是数据包的长度.请注意,上面的参数是"-l" ...
- [Elasticsearch] ES 的Mapping 设计在实际场景中应用
背景 项目中有个需求是需要几个字段作为标签,统计各个标签的文档数量,同时支持分词后的全文检索功能. 原有的mapping设计: curl -XPUT http://ip:9200/meta_es_me ...