a、条件判断where

select * from 表 where id > 1 and name != 'alex' and num = 12;
select * from 表 where id between 5 and 16;
select * from 表 where id in (11,22,33)
select * from 表 where id not in (11,22,33)
select * from 表 where id in (select nid from 表)

b、多条件查询or

select * from 表 where 列1='' or 列2='';

c、通配符like(模糊查找、关键字查找)

select * from 表 where name like 'liu%'  # liu开头的所有(多个字符串)
select * from 表 where name like 'liu_' #liu开头的所有(一个字符)
select * from 表 where name like '%liu_'
select * from 表 where name like '_liu_'
select * from 表 where name like '%liu%'
select * from 表 where name like '_liu%'

c、限制limit(分页)

也可以用在选取数据,比如说隔几行取及行数据。

select * from 表 limit 5;            - 前5行
select * from 表 limit 4,5; - 从第4行开始的5行
select * from 表 limit 5 offset 4 - 从第4行开始的5行

d、排序 order by ,asc,desc

select * from 表 order by 列 asc              - 根据 “列” 从小到大排列
select * from 表 order by 列 desc - 根据 “列” 从大到小排列
select * from 表 order by 列1 desc,列2 asc - 根据 “列1” 从大到小排列,如果相同则按列2从小到大排序

e、范围查询 between  and

select * from 表 where 列名 between 数1 and 数2    --列名在数1和数2之间的 

f、离散查询 in

select * from 表 where price=30 or price=40 or price=50 or price=60  --价格为30或40或50或60的 
select * from 表 where price in(30,40,50,60)  --价格为30或40或50或60的

g、聚合查询 count()、sum()、avg()、max()、min()

select count(*) from 表名   --表里面总共有多少列
select count(code) from 表名   --表里面code有多少列
select sum(price) from 表名   --表里面价格的和
select avg(price) from 表名   --表里面价格的平均价格
select max(price) from 表名   --表里面最高的价格
select min(price) from 表名   --表里面最低的价格

h、去重查询 distinct

select distinct brand from 表名 --去掉重复的brand列

i、分组group by

select num from 表 group by num
select num,nid from 表 group by num,nid
select num,nid from 表 where nid > 10 group by num,nid order by nid desc
select num,nid,count(*),sum(score),max(score),min(score) from 表 group by num,nid
select num from 表 group by num having max(id) > 10 特别的:group by 必须在where之后,order by之前

j、组合union、union all

组合,自动处理重合(去掉相同)
select nickname from A union select name from B 组合,不处理重合(所有的都显示)
select nickname from A union all select name from B

k、连表join

无对应关系则不显示
select A.num, A.name, B.name from A,B Where A.nid = B.nid 无对应关系则不显示
select A.num, A.name, B.name from A inner join B
on A.nid = B.nid A表所有显示,如果B中无对应关系,则值为null
select A.num, A.name, B.name from A left join B on A.nid = B.nid B表所有显示,如果B中无对应关系,则值为null
select A.num, A.name, B.name from A right join B on A.nid=B.nid

MYSQL查找总结的更多相关文章

  1. 使用 MySQL 查找附近的位置

    使用 MySQL 查找附近的位置 以下 SQL 语句将会在与坐标 37, -122 相距 25 英里的半径范围内查找最近的 20 个位置.该语句根据行的纬度/经度以及目标纬度/经度计算距离,然后只请求 ...

  2. Mysql查找如何判断字段是否包含某个字符串

    Mysql查找如何判断字段是否包含某个字符串   有这样一个需求,在Mysql数据库字符串字段(权限)中,用户有多个不同的邮箱,分别被‘,’分开,现在要取出某个邮箱的所有成员列表.   假设有个表: ...

  3. 何在mysql查找效率慢的SQL语句?

    如何在mysql查找效率慢的SQL语句呢?这可能是困然很多人的一个问题,MySQL通过慢查询日志定位那些执行效率较低的SQL 语句,用--log-slow-queries[=file_name]选项启 ...

  4. 好记性不如烂笔头-Mysql查找如何判断字段是否包含某个字符串

    好记性不如烂笔头-Mysql查找如何判断字段是否包含某个字符串 利用mysql 字符串函数 find_in_set(); SELECT * FROM users WHERE find_in_set(' ...

  5. Mysql 查找表中的多组前n大元素

    博客已搬家,更多内容查看https://liangyongrui.github.io/ Mysql 查找表中的多组前n大元素 如果时单组很简单,只需要排序后去前n个就行了,但是多组排序似乎就不是那么好 ...

  6. Mysql查找所有项目开始时间比之前项目结束时间小的项目ID

    这是之前遇到过的一道sql面试题,供参考学习: 查找所有项目开始时间比之前项目结束时间小的项目ID mysql> select * from t2; +----+---------------- ...

  7. mysql查找字符串出现位置

    MySQL中的LOCATE和POSITION函数使用方法 FIND_IN_SET(str,strlist) 假如字符串str 在由N 子链组成的字符串列表strlist 中,则返回值的范围在 1 到 ...

  8. (转载)Mysql查找如何判断字段是否包含某个字符串

    (转载)http://www.th7.cn/db/mysql/201306/31159.shtml 有这样一个需求,在Mysql数据库字符串字段(权限)中,用户有多个不同的邮箱,分别被‘,’分开,现在 ...

  9. mysql查找以逗号分隔的值-find_in_set

    有了FIND_IN_SET这个函数.我们可以设计一个如:一只手机即是智能机,又是Andriod系统的. 比如:有个产品表里有一个type字段,他存储的是产品(手机)类型,有 1.智能机,2.Andri ...

  10. MySQL查找出重复的记录

    问题 查找表中多余的重复记录,重复记录是根据单个字段来判断的.例如:有张表中有uid和uname两个字段,现在需要查找出uname重复的所有数据列.数据表如下: id o_id uname 1 11 ...

随机推荐

  1. OSG学习:基本几何体绘制示例

    绘制并渲染几何体主要有如下3大步骤: 1.创建各种向量数据,如顶点.纹理坐标.颜色和法线等.需要注意的是,添加顶点数据时主要按照逆时针顺序添加, 以确保背面剔除的正确. 2.实例化一个几何体对象(os ...

  2. DataSet和List 泛型之间互相转换 (转载)

    //DataSet与泛型集合间的互相转换 //利用反射机制将DataTable的字段与自定义类型的公开属性互相赋值. //注意:从DataSet到IList<T>的转换,自定义类型的公开属 ...

  3. 使用WCF上传数据

    通过传递Stream对象来传递大数据文件,但是有一些限制: 1.只有 BasicHttpBinding.NetTcpBinding 和 NetNamedPipeBinding 支持传送流数据. 2. ...

  4. JSON字符串书写

      { "XXX公司": [ { "name": "IT部", "mebers": [ { "维护人员&quo ...

  5. OBJ文件

    OBJ文件是Alias|Wavefront公司为它的一套基于工作站的3D建模和动画软件"Advanced Visualizer"开发的一种标准3D模型文件格式,很适合用于3D软件模 ...

  6. 【bzoj4417】[Shoi2013]超级跳马 矩阵乘法

    题目描述 现有一个n行m列的棋盘,一只马欲从棋盘的左上角跳到右下角.每一步它向右跳奇数列,且跳到本行或相邻行.跳越期间,马不能离开棋盘.例如,当n = 3, m = 10时,下图是一种可行的跳法.   ...

  7. 【bzoj1738】[Usaco2005 mar]Ombrophobic Bovines 发抖的牛 Floyd+二分+网络流最大流

    题目描述 FJ's cows really hate getting wet so much that the mere thought of getting caught in the rain m ...

  8. C++中swap函数

    本文是我用到swap函数时,对其产生好奇,所以结合网上有关博文写下的.个人水平有限,若有错误的地方,欢迎留言指出.谢谢! 一.通用的函数交换模板 template<class T> voi ...

  9. js判断设备类型

    1. 判断微信 function is_weixin() { var ua = window.navigator.userAgent.toLowerCase(); if (ua.match(/Micr ...

  10. angular 前台代码分层方法

    原代码: 现在将 findAll的get请求部分抽取成 服务,服务就是 $http.get 其实就是 ang内置的服务,其实就是可能会公用的方法,即可能被多个控制器调用的方法 比如这里认为 get请求 ...