mysql(三):mysql查询语句 和 多表关联查询 以及 子查询

1.查询一张表: select * from 表名;
2.查询指定字段:select 字段1,字段2,字段3….from 表名;
3.where条件查询:select 字段1,字段2,字段3 frome 表名 where 条件表达式;
例:select * from t_studect where id=1;
select * from t_student where age>22;
4.带in关键字查询:select 字段1,字段2 frome 表名 where 字段 [not]in(元素1,元素2);
例:select * from t_student where age in (21,23);
select * from t_student where age not in (21,23);
5.带between and的范围查询:select 字段1,字段2 frome 表名 where 字段 [not]between 取值1 and 取值2;
例:select * frome t_student where age between 21 and 29;
select * frome t_student where age not between 21 and 29;
6.带like的模糊查询:select 字段1,字段2… frome 表名 where 字段 [not] like ‘字符串’;
“%”代表任意字符;
“_”代表单个字符;
例:select * frome t_student where stuName like ‘张三”;
select * frome t_student where stuName like ‘张三%”;
select * frome t_student where stuName like ‘%张三%”;//含有张三的任意字符
select * frome t_student where stuName like ‘张三_”
7.空值查询:select 字段1,字段2…frome 表名 where 字段 is[not] null;
8.带and多条件查询:
select 字段1,字段2…frome 表名 where 条件表达式1 and 条件表达式2 [and 条件表达式n]
例:select * frome t_student where gradeName=’一年级’ and age=23;
9.带or的多条件查询
select 字段1,字段2…frome 表名 where 条件表达式1 or 条件表达式2 [or 条件表达式n]
例:select * frome t_student where gradeName=’一年级’ or age=23;//或者,条件只要满足一个
10.distinct去重复查询:select distinct 字段名 from 表名;
11.对查询结果排序order by:select 字段1,字段2…from 表名 order by 属性名 [asc|desc]
例:select * frome t_student order by age desc;//降序,从大到小
select * frome t_student order by age asc;//升序,asc默认可以不写
12.分组查询group by
group by 属性名 [having 条件表达式][with rollup]
1.单独使用(毫无意义,不能单独使用);
2.与group_concat()函数一起使用;
例:select gradeName,group_concat(stuName) from t_student group by gradeName;

3.与聚合函数一起使用;
例:select gradeName,count(stuName) from t_student group by gradeName;

4.与having一起使用(显示输出的结果);
例:select gradeName,count(stuName) from t_student group by gradeName having count(stuName)>3;

5.与with rollup 一起使用(最后加入一个总和行);
例:select gradeName,group_concat(stuName) from t_student group by gradeName with rollup;

13.limit 分页查询:select 字段1,字段2,…from 表名 limit 初始位置,记录数;
例子:select * from t_student limit 0,5;多表连接查询
表一:t_book

表二:t_bookType

表三:t_priceLevel

select * from t_book,t_bookType;

1.内连接查询(两张或以上的表连接起来查询需要的数据)
根据表一的bookTypeId查询出所有bookTypeName
select * from t_book,t_bookType where t_book.bookTypeId=t_bookType.id;

查询某几个字段:
select bookNme,author from t_book,t_bookType where t_book.bookTypeId=t_bookType.id;

2.外连接查询(两张或以上的表连接起来查询某张表的信息)
3.左连接查询
select * from t_book left join t_bookType on t_book.bookTypeId=t_bookType.id;
如下图:表一(左边表)t_book的数据全部查出 表二没有的字段用null代替

4.右连接查询
select * from t_book right join t_bookType on t_book.bookTypeId=t_bookType.id;
查出表二(右边表)的所有信息,表一没有的用null代替

5.多条件连接查询
select * from t_book,t_bookType where t_book.bookTypeId=t_bookType.id and t_book.price>70;

子查询
1.带in关键字的子查询(一个查询语句的条件可能落在另一个select语句的查询结果中)
select * from t_book where bookType in(select id from t_bookType);
select * from t_book where bookType not in(select id from t_bookType);
2.带比较运算符的子查询(子查询可以使用比较运算符)
select * from t_book where price>=(select price from t_priceLevel where priceLevel=1);
3.带exists关键字的子查询(加入子查询查询到记录,则进行外层查询,否则,不执行外层查询)
select * from t_book where exists(select * from t_booktype);
select * from t_book where not exists(select * from t_booktype);
4.带any关键字的子查询(any关键字表示满足其中任一条件)
select * from t_book where price>= any(select price from t_priceLevel);
5.带all关键字的子查询(all关键字表示满足所有条件)
select * from t_book where price>= all(select price from t_priceLevel);
合并查询
1.union
使用union关键字是,数据库系统会将所有的查询结果合并到一起,然后去掉相同的记录;
select id from t_book union select id from t_bookType;
2.union all
使用union all,不会去除掉重复的记录;
select id from t_book union all select id from t_bookType;

mysql not in语句_MySQL命令相关推荐

  1. mysql命令行语句_MySql命令行命令和SQL语句

    一.常用mysql命令行命令 1.启动MYSQL服务 net start mysql 停止MYSQL服务 net stop mysql 2.netstat -na|findstr 3306 查看被监听 ...

  2. mysql数据库管理系统模式_MYSQL命令行模式管理MySql的一点心得

    MYSQL命令行模式管理MySql的一点心得 MYSQL命令行模式管理MySql的一点心得 MySql数据库是中小型网站后台数据库的首选,因为它对非商业应用是免费的.网站开发者可以搭建一个" ...

  3. mysql的各种语句_MySQL 常用语句大全

    MySQL 常用语句大全 一.连接 MySQL 格式: mysql -h 主机地址 -u 用户名 -p 用户密码 1.例 1:连接到本机上的 MYSQL. 首先在打开 DOS 窗口,然后进入目录 my ...

  4. shell实行mysql语句_【Mysql】shell运行mysql的sql语句_MySQL

    bitsCN.com [Mysql]shell运行mysql的sql语句 shell本身是一种脚本语言,所以不能像java一样通过api去连接数据库.shell还是要借助mysql本身的一些运行脚本才 ...

  5. mysql的操作语句_Mysql最常用的操作语句收集

    Mysql中常用语句简单易学 springboot微服务是现在流行的框架,目前大多数做java的人都在使用,java的生态一直很好,各种插件各种第三方jar包推动着java的运行.Mysql是Spri ...

  6. mysql常用的语句_MySQL常用语句集锦

    //select *from..主表..left join ..次表..on..主表.字段IDax=次表.字段IDbx where 主表.字段x=变量1 and 次表.字段x2 like '%变量2% ...

  7. mysql的repeat语句_mysql实例 repeat语句的用法

    本节分享一段mysql实例代码,学习mysql中repeat语句的用法. 代码如下: mysql> DELIMITER // mysql> CREATE FUNCTION myFuncti ...

  8. mysql一些常用语句_MySQL常用语句

    一.前言 今天天气很好,大晴天,心情也好好的.就将MySQL常用的语句总结一下,记录在随笔里,也顺便分享分享.日后,这篇随笔我将会持续更新,作为我自己的MySQL语句大全. 二.常用SQL语句 我将由 ...

  9. mysql修改索引语句_mysql——创建索引、修改索引、删除索引的命令语句

    查看表中已经存在 index:show index from table_name; 创建和删除索引索引的创建可以在CREATE TABLE语句中进行,也可以单独用CREATE INDEX或ALTER ...

最新文章

  1. 使用metasploit中Evasion模块
  2. 开发nagios插件监控/etc/passwd中文件变化
  3. SpringBoot配置嵌入式Servlet容器
  4. java架构程序员月入破3万到底是怎么炼成的,一篇文章让你了解
  5. RxJava 2.x 教程
  6. postman返回值设置为全局变量
  7. Java并发教程–阻塞队列
  8. 精心总结了10个matplotlib绘图技巧,短小精悍,威力无穷!
  9. 我安装java了_我安装了JAVA为什么.......
  10. 对变量移位顺序读写_Java多线程并发读写锁ReadWriteLock实现原理剖析
  11. 《Kotlin项目实战开发》第1章 Kotlin是什么 1
  12. 微型计算机三级项目,微机原理三级项目.doc
  13. 长方体空间移动工程师_打破常规,私人定制移动阳光房
  14. nested exception is org.apache.ibatis.executor.ExecutorException: No constructor found in com.chengg
  15. 平面设计好学吗?没有基础学平面设计难吗?
  16. CPC是什么意思和CPM、CPV有什么不同?
  17. 淘宝搜索上传图片获得上传sid
  18. 程序猿开启竖屏之路(竖屏的安装和使用)
  19. 【数据湖Hudi的概念】Table Types、Indexing和Metadata Table
  20. 白岩松:不平静,就不会幸福

热门文章

  1. 前端不为人知的一面--前端冷知识集锦
  2. 使用存储过程创建分页
  3. Reactive Extensions简介一
  4. 线上分享 | 数据产品经理:如何突破现状,更进一层?
  5. 导航菜单设计五步法——B端设计指南
  6. 记住:用户不是傻*,她是你的老婆大人
  7. 移动应用用户使用成本控制管理
  8. 常用binlog日志操作命令
  9. 币安被赶走,交易所寒冬将至?
  10. [Jenkins]Error:403 No valid crumb was included in the request