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 |

+------+------+----------+

例子查询名字等于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-单表查询的更多相关文章

  1. mysql 基础入门 单表查询

    单表查询 select 表头,表头 as 别名 ,表头(+-*/的运算) from table_a 1.条件查询 where + 条件 <> , != 不等于 = 等于,也可以表示字符串值 ...

  2. mysql 数据操作 单表查询 目录

    mysql 数据操作 单表查询 mysql 数据操作 单表查询 简单查询 避免重复DISTINCT mysql 数据操作 单表查询 通过四则运算查询 mysql 数据操作 单表查询 concat()函 ...

  3. mysql 数据操作 单表查询 where 约束 目录

    mysql 数据操作 单表查询 where约束 between and or mysql 数据操作 单表查询 where约束 is null in mysql 数据操作 单表查询 where约束 li ...

  4. mysql 数据操作 单表查询 group by 分组 目录

    mysql 数据操作 单表查询 group by 介绍 mysql 数据操作 单表查询 group by 聚合函数 mysql 数据操作 单表查询 group by 聚合函数 没有group by情况 ...

  5. MySQL数据库之单表查询中关键字的执行顺序

    目录 MySQL数据库之单表查询中关键字的执行顺序 1 语法顺序 2 执行顺序 3 关键字使用语法 MySQL数据库之单表查询中关键字的执行顺序 1 语法顺序 select distinct from ...

  6. MySQL数据库语法-单表查询练习

    MySQL数据库语法-单表查询练习 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 本篇博客主要是对聚合函数和分组的练习. 一.数据表和测试数据准备 /* @author :yinz ...

  7. MySQL学习9 - 单表查询

    一.单表查询的语法 二.关键字的执行优先级(重点) 三.单表查询示例 1.where约束 2.group by分组查询 3.聚合函数 4.HAVING过滤 5.order by查询排序 6.limit ...

  8. mysql四-1:单表查询

    一 单表查询的语法 SELECT 字段1,字段2... FROM 表名 WHERE 条件 GROUP BY field HAVING 筛选 ORDER BY field LIMIT 限制条数 二 关键 ...

  9. mysql数据库之单表查询多表查询

    单表查询 前期表准备 create table emp( id int not null unique auto_increment, name varchar(20) not null, sex e ...

  10. Python、mysql四-1:单表查询

    一 单表查询的语法 SELECT 字段1,字段2... FROM 表名 WHERE 条件 GROUP BY field HAVING 筛选 ORDER BY field LIMIT 限制条数 二 关键 ...

随机推荐

  1. 【编程思想】【设计模式】【行为模式Behavioral】chaining_method

    Python版 https://github.com/faif/python-patterns/blob/master/behavioral/chaining_method.py #!/usr/bin ...

  2. ES6——>let,箭头函数,this指向小记

    let let允许你声明一个作用域被限制在块级中的变量.语句或者表达式. 还是那个经典的问题:创建5个li,点击不同的li能够打印出当前li的序号. 如果在for循环中使用**var**来声明变量i的 ...

  3. Spring Cloud Eureka源码分析之服务注册的流程与数据存储设计!

    Spring Cloud是一个生态,它提供了一套标准,这套标准可以通过不同的组件来实现,其中就包含服务注册/发现.熔断.负载均衡等,在spring-cloud-common这个包中,org.sprin ...

  4. Flutter 中如何优雅的实现多渠道打包(埋点统计系列)

    我是 Zero,脑图先奉上 先赞后看,更新永不断 只要你关注 Flutter,这篇文章你绝对用得着,==> 强烈建议收藏 多渠道打包介绍 多渠道打包的主要作用是满足产品的运营需求,统计渠道和活动 ...

  5. Jenkins构建通知

    目录 一.简介 二.推送到gitlab 三.邮件通知 自带配置 Email Extension 四.钉钉通知 五.脚本钉钉通知 六.HTTP请求通知 一.简介 类似于监控报警,jenkins在配置持续 ...

  6. Jenkins 关闭和重启的实现方式

    关闭jenkins 只需要在访问jenkins服务器的网址url地址后加上exit.例如我jenkins的地址http://localhost:8080/ , 那么我只需要在浏览器地址栏上敲下 htt ...

  7. GSS API

    Detail:http://docs.oracle.com/cd/E24847_01/html/E22200/overview-61.html GSS-API 介绍 使用 GSS-API,程序员在编写 ...

  8. matplotlib模块详解

    简单绘图,折线图,并保存为图片 import matplotlib.pyplot as plt x=[1,2,3,4,5] y=[10,5,15,10,20] plt.plot(x,y,'ro-',c ...

  9. centos7 ssh 提示/bin/bash No such file or directory 【ldd命令理解】

    现象:客户报障ssh无法登陆.提示/bin/bash No such file or directory 排查:进入单用户模式 linux16 行ro替换 rw init=/sysroot/bin/s ...

  10. textarea控件好奇怪啊,用<s:if>标签居然不在一行回出现很多的空格,奇葩啊

    textarea控件好奇怪啊,用<s:if>标签居然不在一行回出现很多的空格,奇葩啊