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. lintcode-178-图是否是树

    178-图是否是树 给出 n 个节点,标号分别从 0 到 n - 1 并且给出一个 无向 边的列表 (给出每条边的两个顶点), 写一个函数去判断这张`无向`图是否是一棵树 注意事项 你可以假设我们不会 ...

  2. 【alpha】Scrum站立会议第2次....10.17

    小组名称:nice! 小组成员:李权 于淼 杨柳 刘芳芳 项目内容:约跑app 1.任务进度 成员 已完成 今日完成 李权 数据库设计 消息发送代码实现 于淼 注册.登录界面,以及登录界面后台代码.发 ...

  3. 高性能python

    参考来源:Python金融大数据分析第八章 提高性能有如下方法 1.Cython,用于合并python和c语言静态编译泛型 2.IPython.parallel,用于在本地或者集群上并行执行代码 3. ...

  4. Kafka性能之道

    Kafka高性能之道 高效使用磁盘 零拷贝 批处理和压缩 Partition ISR 高效使用磁盘 >顺序写cipan >Append Only(数据不更新,无记录级的数据删除,只会整个s ...

  5. C# Directory.GetFiles()获取文件时如果是根目录时有隐藏文件则报错的处理

    如果Directory.GetFiles("d:\"),则由于回收站是隐藏文件而报错,怎么躲避这种错误呢, 我要了一种办法,只要遇到隐藏文件夹就跳过的方法: foreach (va ...

  6. poj 1469 COURSES (二分匹配)

    COURSES Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 16877   Accepted: 6627 Descript ...

  7. 【转】c# 类反射简单操作

    转:http://www.jb51.net/article/25863.htm 首先建立一个测试的类  复制代码代码如下: public class MyClass { public int one ...

  8. 一条数据的HBase之旅,简明HBase入门教程-Write全流程

    如果将上篇内容理解为一个冗长的"铺垫",那么,从本文开始,剧情才开始正式展开.本文基于提供的样例数据,介绍了写数据的接口,RowKey定义,数据在客户端的组装,数据路由,打包分发, ...

  9. BZOJ2657:[ZJOI2012]旅游——题解

    http://www.lydsy.com/JudgeOnline/problem.php?id=2657 https://www.luogu.org/problemnew/show/P2610 到了难 ...

  10. HDU3157:Crazy Circuits——题解

    http://acm.hdu.edu.cn/showproblem.php?pid=3157 题目大意:给一个电路 ,起点为+,终点为-,包括起点终点在内的电元件之间有有下界边,求最小流. ————— ...