-- create table students (
-- studentNo varchar(10) primary key,
-- name varchar(10),
-- sex varchar(1),
-- hometown varchar(20),
-- age tinyint(4),
-- class varchar(10),
-- card varchar(20)
--  )
-- insert into students values
-- ('001', '王昭君', '女', '北京', '20', '1班', '340322199001247654'),
-- ('002', '诸葛亮', '男', '上海', '18', '2班', '340322199002242354'),
-- ('003', '张飞', '男', '南京', '24', '3班', '340322199003247654'),
-- ('004', '白起', '男', '安徽', '22', '4班', '340322199005247654'),
-- ('005', '大乔', '女', '天津', '19', '3班', '340322199004247654'),
-- ('006', '孙尚香', '女', '河北', '18', '1班', '340322199006247654'),
-- ('007', '百里玄策', '男', '山西', '20', '2班', '340322199007247654'),
-- ('008', '小乔', '女', '河南', '15', '3班', null),
-- ('009', '百里守约', '男', '湖南', '21', '1班', ''),
-- ('010', '妲己', '女', '广东', '26', '2班', '340322199607247654'),
-- ('011', '李白', '男', '北京', '30', '4班', '340322199005267754'),
-- ('012', '孙膑', '男', '新疆', '26', '3班', '340322199000297655')
-- where 条件查询

--    比较运算符
--     等于: =
--     大于: >
--     大于等于: >=
--     小于: <
--     小于等于: <=
--     不等于: != 或 <>
-- select age from students where name='小乔'   (查询小乔的年龄)
-- select * from students where age<20          (查询20岁以下的学生)
-- select * from students where hometown!='北京' (查询家乡不在北京的学生)
-- select card from students where studentNo='007'(查询学号是007的学生的身份证号)
-- select * from students where class!='1班' (查询一班以外的学生信息)
-- select name,sex from students where age>20 (查询年龄大于20的学生的姓名和性别)

-- 逻辑运算符
--
--     and
--     or
--     not

-- select * from students where age<20 and sex='女' (查询年龄小于20的女同学)
-- select * from students where sex='女' or class='1班' (查询女学生或'1班'的学生)
-- select * from students where not hometown='天津'   (查询非天津的学生)
-- select * from students where hometown='河南' OR hometown= '河北' (查询河南或河北的学生)
-- select from students where class='1班'and hometown='上海'  (查询'1班'的'上海'的学生 )
-- select * from students where not age=20   (查询非20岁的学生)

-- 模糊查询
--     like
--     %表示任意多个任意字符
--     _表示一个任意字符
--
-- select * from students where name like '孙%' (查询姓孙的学生)
-- select * from students where name like '孙_'(查询姓孙且名字是一个字的学生)
-- select * from students where name like '%乔'(查询叫乔的学生)
-- select * from students where name like '%白%'(查询姓名含白的学生)
-- select * from  students where name like '__'    (查询姓名为两个字的学生)
-- select * from students where name like '百%'and age>20 (查询姓百且年龄大于20的学生)
-- select * from students where studentNo like '%1' (查询学号以1结尾的学生)

-- 范围查询
--
--     in表示在一个非连续的范围内 between ... and ...表示在一个连续的范围内

-- select * from students where hometown in('北京','上海','广东')(查询家乡是北京或上海或广东的学生)
-- select * from students where age between 18 and 20(查询年龄为18至20的学生)
-- select *from students where age in(18,19,22)and sex='女' (查询年龄在18或19或22的女生)
-- select * from students where not age between 20 and 25 (查询年龄在20到25以外的学生)

-- 空判断
--
--     注意:null与''(字符串)是不同的
--     判空is null
--         判非空is not null

-- select * from students where card is null (查询没有填写身份证的学生)
-- select * from students where card ='' (查询身份证是空字符串的学生)
-- select * from students where card  is not null (填写身份证的学生)

-- 排序 order by
-- 默认按照列值从小到大排列
-- asc从小到大排列,即升序
-- desc从大到小排序,即降序
--
-- select * from students order by age(查询所有学生信息,按年龄从小到大排序)
-- select * from students order by age desc,studentNo (查询所有学生信息,按年龄从大到小排序,年龄相同时,再按学号从小到大排序)
-- select * from students order by class asc,studentNo (查询所有学生信息,按班级从小到大排序,班级相同时,再按学号再按学号从小到大排序)

-- 聚合函数  count(*)表示计算总行数 max最大 min 最小 sum求和 avg 平均

-- select count(*) from students;     (查询学生总数)
-- select max(age) from students where sex='女'(查询女生的最大年龄)
-- select min(age) from students where class=1(查询1班的最小年龄)
-- select sum(age) from students where hometown='北京'(查询北京学生的年龄总和)
-- select avg(age) from students where sex='女'(查询女生的平均年龄)
-- select max(age),min(age),avg(age)from students;(查询所有学生的最大年龄、最小年龄、平均年龄)
-- select count(*)from students where class=1; (一班共有多少个学生)
-- select count(*) from students where age<18 and class=3(查询3班年龄小于18岁的同学有几个)

-- 分组 group by 筛选分组数据 having 与where用法一样

-- select sex,count(*)from students group by sex 查询各种性别的人数
-- select age,count(*)from students group by age(查询各种年龄的人数)
-- select class,avg(age),max(age),min(age) from students group by class(查询各个班级学生的平均年龄、最大年龄、最小年龄)
-- select sex,count(*) from students group by sex having sex='男'(查询男生总人数)
-- select class,avg(age),max(age),min(age) from students group by class having class!=1(查询1班除外其他班级学生的平均年龄、最大年龄、最小年龄)
-- select class,avg(age),max(age),min(age) from students where class!='1班' group by class  (查询1班除外其他班级学生的平均年龄、最大年龄、最小年龄)

-- 分页 limit
-- select * from students limit 0,3(查询前3行学生信息)
-- select * from students limit 3,3 (查询第4到第6行学生信息)

-- 查询 顺序
-- select *
-- from 表名
-- where 条件1
-- group by 依据列
-- having 条件2
-- oeder by 依据列
-- limit 0,1
-- select class,max(age),min(age)from students where class !='1班' group by class order by class desc  limit 0,3

-- select * from students where name ='百里守约'(查询学生"百里守约"的基本信息)
-- select * from students where name ='百里守约'or name ='百里玄策' (查询学生"百里守约"或”百里玄策”的基本信息)
-- select name,age,class from students where name like '张%'(查询姓"张"学生的姓名,年龄,班级)
-- select *from students where name like '%约%'1(查询姓名中含有"约"字的学生的基本信息)
-- select studentno,name,age,class,card from students where name like '孙__' (查询姓名长度为三个字,姓“孙”的学生的学号,姓名,年龄,班级,身份证号)
-- select *from students where name like '百%' or '孙'(查询姓"百"或者姓”孙”的学生的基本信息)
-- select * from students where name like '百%'and hometown='山西'(查询姓"百"并且家乡是"山西"的学生信息)
-- select * from students where hometown in('北京','上海','新疆','山东') (查询家乡是"北京"、”新疆”、”山东”或者"上海"的学生的信息)
-- select * from students where name like '孙%'and hometown!='河北'(查询姓"孙",但是家乡不是"河北"的学生信息)
-- select * from students where hometown not in('北京','新疆','山东','上海') (查询家乡不是"北京"、"新疆"、"山东"、"上海"的学生的信息)
-- select * from students where hometown!='北京'and hometown!='上海'and hometown!='新疆'and hometown!='山东'(查询家乡不是"北京"、"新疆"、"山东"、"上海"的学生的信息)
-- select * from students order by sex (查询全部学生信息,并按照“性别”排序)
-- select name,hometown  from students group by hometown (查询现有学生都来自于哪些不同的省份)
-- select * from students group  by age having sex='男'(查询所有男生,并按年龄升序排序)
-- select  count(*) from students(统计共有多少个学生)
-- select * from students where age>20(统计年龄大于20岁的学生有多少个)
-- select avg(age)from students where sex='男'(统计男生的平均年龄)
-- select max(age) from students where class=1(、查询1班学生中的最大年龄是多少)
-- select sex,count(*) from students where class='2班' group by sex (统计2班男女生各有多少人)
-- select class,sex,count(*) from students group by class,sex order by class 1(统计每个班级中每种性别的学生人数,并按照班级升序排序)
-- select * from students order by age limit 1(查询年龄最小的学生的全部信息)

Mysql 查询 select相关推荐

  1. MySQL查询select实例 【笔记】

    use mydb; select * from EMP; select * from DEPT; select DISTINCT JOB from EMP;  -- distinct   去除重复项 ...

  2. MySQL查询select语句详解

    1.查询记录 select*from 表名 [where 条件];eg:select*from students;//查询 students 表中所有记录,所有字段的值都显示出来select fiel ...

  3. MySql查询——Select

    数据库的查询,我们从表里选出我们想要的数据,查询结构仍是一个表 查询时,我们需要使用select语句,配合 from where order by group by having 除了这些子句使用,还 ...

  4. Java mysql获取行数_java – MySQL查询获取球体中的行(X,Y,Z坐标)?

    我正在制作一个名为Minecraft with Bukkit API的游戏插件. 我有一个名为Reinforcements的数据库表,其中包含以下字段:x integer,y integer,z in ...

  5. mysql的覆盖语句_求教一个mysql查询问题:为什么我的SQL语句不能覆盖所有情况?内详...

    mysql 查询 select count(*) from t1 where col1 > 0 339074条 select count(*) from t1 where col1 > 0 ...

  6. mysql之select查询

    SELECT查询数据 单表查询 语法 SELECT 字段1,字段2... FROM 表名WHERE 条件GROUP BY fieldHAVING 筛选ORDER BY fieldLIMIT 限制条数 ...

  7. mysql select time,MySql查询时间段的方法

    本文实例讲述了MySql查询时间段的方法.分享给大家供大家参考.具体方法如下: MySql查询时间段的方法未必人人都会,下面为您介绍两种MySql查询时间段的方法,供大家参考. MySql的时间字段有 ...

  8. mysql 查询指定字段数据_MySQL使用select语句查询指定表中指定列(字段)的数据

    本文介绍mysql数据库中执行select查询语句,查询指定列的数据,即指定字段的数据. 再来回顾一下sql语句中的select语句的语法: select 语句的基本语法: select from w ...

  9. mysql一秒查询次数_单个select语句实现MySQL查询统计次数

    单个select语句实现MySQL查询统计次数 单个select语句实现MySQL查询统计次数的方法用处在哪里呢?用处太多了,比如一个成绩单,你要查询及格得人数与不及格的人数,怎么一次查询出来? My ...

最新文章

  1. Ext之Combobox的远程加载数据实例(附前后台代码)
  2. linux more 下一页_Linux学习笔记
  3. php网站分区,PHP - Manual: 分区和分片 (官方文档)
  4. 女神节快乐!世界上第一位程序员就是女神
  5. 项目解析jsx文件_神奇了!这个 Go 语言项目让前端构建快了近 100 倍
  6. Tomcat服务器上Servlet连接数据库连接不上出现空指针异常的解决方案
  7. centos 5.6 安装redmine 步骤
  8. 如何取消选中单选按钮?
  9. 图像直方图规定化(Specification)数学原理和纯C语言实现
  10. Stata:何时使用线性概率模型而非Logit?
  11. C#实现的基于SMTP协议的E-MAIL电子邮件发送客户端软件
  12. 一文读懂高频交易程序化交易和量化交易区别
  13. U盘中的文件夹都变成了.exe文件
  14. WeakHashMap源码解析及使用场景
  15. 记录一次关于解除推特的冻结
  16. 飞檐走壁已经不是成龙甄子丹们的特技了,TA也可以做到!
  17. Excel客户等级星级表达
  18. linux安装vlc只能命令行启动,Ubuntu安装多功能的媒体播放器VLC播放器的步骤
  19. 判断字符串中的字母大小写、数字
  20. php curl访问HTTPS页面502

热门文章

  1. 中国人工智能城市排名榜TOP 10公布;宁德时代将授权现代摩比斯使用CTP技术 | 美通社头条...
  2. 如何将谷歌浏览器安装在D盘
  3. python丨制表符丨对齐
  4. 联想服务器修改启动模式,联想bios 启动模式设置教程
  5. 【物联网】CoAP协议总结
  6. ITK-SNAP + c3d 处理 3D 图像以及 DICOM 格式
  7. 移动终端web开发必备知识
  8. arr 安卓调用qmui_QMUI(Android)炒鸡简单的配置详解
  9. xp升级win7_PDF编辑软件强势来袭!Acrobat XI升级版 Acrobat DC 安装教程
  10. 2013,年度十大互联网营销事件挨个评说