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 |

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

最新文章

  1. PeerConnection
  2. fnd_profile.value('AFLOG_ENABLED')的取值 和配置文件相关SQL
  3. 货币金额的计算 - Java中的BigDecimal
  4. android adt自带eclipse无法设置ndk路径(找不到NDK配置)
  5. 用gd库画矩形和椭圆
  6. NERDTree这个插件的用法简介
  7. 201521123104 《Java程序设计》第6周学习总结
  8. 201521123106《java程序设计》第四周学习总结
  9. js和jquery通过this获取html标签中的属性值
  10. Java项目专栏之数据库建表
  11. Java学习笔记(二)事件监听器
  12. Android视频录制从不入门到入门系列教程(三)————视频方向
  13. java 多线程 synchronized与lock的通信机制等问题,结合相应实例说明
  14. 【转载】Google 程序员消灭 Bug 的 5 大法宝!
  15. 在vue项目中引入jquery
  16. LVS+Keepalived+Mysql+主主数据库架构[2台]
  17. SQL-36 创建一个actor_name表,将actor表中的所有first_name以及last_name导入改表。
  18. python threading模块
  19. Node.js实战(四)之调试Node.js
  20. Asp.net vNext 学习之路(三)

热门文章

  1. 【编程思想】【设计模式】【行为模式Behavioral】中介者模式Mediator
  2. C语言编辑链接
  3. Windows下80端口被占用的解决方法(SQL Server)
  4. jQuery对象进行方法扩展
  5. mysql安装 报错解决
  6. 莫烦python教程学习笔记——保存模型、加载模型的两种方法
  7. Nginx配置FTP
  8. IO中同步异步,阻塞与非阻塞 -- 原理篇
  9. Linux_ShellCode总结
  10. LET函数(Excel函数集团)