mysql查询语句 和 多表关联查询 以及 子查询
原文地址: http://blog.csdn.net/github_37767025/article/details/67636061
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查询语句 和 多表关联查询 以及 子查询的更多相关文章
- spring-data-jpa 介绍 复杂查询,包括多表关联,分页,排序等
本篇进行Spring-data-jpa的介绍,几乎涵盖该框架的所有方面,在日常的开发当中,基本上能满足所有需求.这里不讲解JPA和Spring-data-jpa单独使用,所有的内容都是在和Spring ...
- 警惕 MySql 更新 sql 的 WHERE 从句中的 IN() 子查询时出现的性能陷阱
警惕 MySql 更新 sql 的 WHERE 从句中的 IN() 子查询时出现的性能陷阱 以下文章来源:https://blog.csdn.net/defonds/article/details/4 ...
- mysql 外键约束及表关联
一.MYSQL中的约束 1.主键:primary key 唯一非空的特性并且可以优化查询速度 2.外键:foreign key 外键的作用保证2个或2个以上的数据表的数据一致性和完整性 3.唯一:un ...
- MySQL高级查询与编程笔记 • 【第3章 子查询】
全部章节 >>>> 本章目录 3.1 子查询定义和单行子查询 3.1.1 子查询定义 3.1.2 单行子查询应用 3.1.4 实践练习 3.2 多行子查询应用 3.2.1 ...
- SQL Fundamentals: 子查询 || WHERE,HAVING,FROM,SELECT子句中使用子查询,WITH子句
SQL Fundamentals || Oracle SQL语言 子查询(基础) 1.认识子查询 2.WHERE子句中使用子查询 3.在HAVING子句中使用子查询 4.在FROM子句中使用子查询 5 ...
- Sql Server的艺术(六) SQL 子查询,创建使用返回多行的子查询,子查询创建视图
子查询或内部查询或嵌套查询在另一个SQL查询的查询和嵌入式WHERE子句中. 子查询用于返回将被用于在主查询作为条件的数据,以进一步限制要检索的数据. 子查询可以在SELECT,INSERT,UPDA ...
- “子查询返回的值不止一个。当子查询跟随在 =、!=、<、<=、>、>= 之后,或子查询用作表达式时,这种情况是不允许的。”SQL查询错误解析
为了实现下述代码,首先得有数据库和相应的表格,本文用的是https://blog.csdn.net/qaz13177_58_/article/details/5575711/中的案例,即先用链接中那些 ...
- insert into select 引起的 "子查询返回的值不止一个。当子查询跟随在**之后,或子查询用作表达式时,这种情况是不允许的"
目录 1.事故现场 1.1 在使用 Insert into Table2 select * from Table1 将表1的数据插入到表2时,报错如下: 1.2 sql 语句 2.推测 3.解决方案 ...
- SQL server 查询出现:---“子查询返回的值不止一个。当子查询跟随在 =、!=、<、<=、>、>= 之后,或子查询用作表达式时,这种情况是不允许的。”SQL查询错误解析---
最近用select进行数据筛选,碰到下面的这个错误: ---子查询返回的值不止一个.当子查询跟随在 =.!=.<.<=.>.>= 之后,或子查询用作表达式时,这种情况是不允许的 ...
随机推荐
- ARC官方文档翻译! - iPhone App开发外包专区 - 威锋论坛 - 威锋网
CHENYILONG Blog ARC官方文档翻译! - iPhone App开发外包专区 - 威锋论坛 - 威锋网 http://bbs.weiphone.com/read-htm-tid-344 ...
- Oracle错误: ORA-01722 无效数字
ORA-01722: 无效数字 主要原因是: 1.对于两个类型不匹配(一个数字类型,一个非数字类型,同下)的值进行赋值操作; 2.两个类型不匹配的值进行比较操作(例如,"="); ...
- 守卫者的挑战(guard)
problem Pro 打开了黑魔法师Vani的大门,队员们在迷宫般的路上漫无目的地搜寻着关押applepi的监狱的所在地.突然,眼前一道亮光闪过.“我,Nizem,是黑魔法圣殿的守卫者.如果你能通过 ...
- Spring4笔记3--Bean的装配
Bean的装配: Bean 的装配,即 Bean 对象的创建.容器根据代码要求创建 Bean 对象后再传递给代码的过程,称为 Bean 的装配. 1. 创建Bean对象的方式: 1. 通过 getBe ...
- select()函数用法一
select()函数用法以及FD_ZERO.FD_SET.FD_CLR.FD_ISSET select函数用于在非阻塞中,当一个套接字或一组套接字有信号时通知你,系统提供select函数来实现多路复用 ...
- textarea保留换行和空格
<style> pre {white-space: pre-wrap;} </style> //替换textare <pre class="feedback_q ...
- springquartz的LocalDataSourceJobStore
spring 为quartz 提供了一个 继承 JobStoreCMT的 LocalDataSourceJobStore,主要是为了和spring更好的集成. public class LocalDa ...
- Oracle 函数 “申请通过后,将该表中循环遍历到的所有内容插到另一个表中”
create or replace function mcode_apply_insert_material(p_mca_no VARCHAR2, p_action VARCHAR2, p_wf_no ...
- 用SQL语句添加删除修改字段、一些表与字段的基本操作、数据库备份等
用SQL语句添加删除修改字段 1.增加字段 alter table docdsp add dspcode char(200) 2.删除字段 ALTER TABLE table_NAME DROP CO ...
- 20155225 2016-2017-2 《Java程序设计》第十周学习总结
20155225 2016-2017-2 <Java程序设计>第十周学习总结 教材学习内容总结 22章网络 22.1网络概览 22.2超文本传输协议(HTTP) 22.2.1 HTTP请求 ...