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-170-旋转链表

    170-旋转链表 给定一个链表,旋转链表,使得每个节点向右移动k个位置,其中k是一个非负数 样例 给出链表1->2->3->4->5->null和k=2 返回4-> ...

  2. iOS- UITableViewCell对象是怎么重用的 ?

    iOS设备的内存有限,如果用UITableView显示成千上万条数据, 就需要成千上万个UITableViewCell对象的话, 那将会耗尽iOS设备的内存.要解决该问题,需要重用UITableVie ...

  3. 3dContactPointAnnotationTool开发日志(十五)

      有时候拖动一个窗口的时候可能直接拖出去了那就再也拖不回来只能reset重新来过:   于是开了个类成员变量在start里记录了一下panel的位置: var lp = panel.GetCompo ...

  4. DVD与CD区别

    经常听朋友说什么DVD什么CD什么的,不知道到底有什么区别,专门百度找了下,找到以下资料 ======================================================= ...

  5. matlab如何将数组中的NAN值去除

        比如我们一组数据,里面有不少的NaN值,如何将其删除掉呢?可以通过find函数来搞定.     我们可以通过importdata('data.txt')将数据文件data.txt导入数组A中. ...

  6. Android Shimmer 发光微光动画

    这是Facebook提供的一个类库(题外话http://code.facebook.com,这里有很多好玩有趣有用的Facebook开源的类库) 这么炫酷的发光动画效果,想必很多Android码农都会 ...

  7. Python字符串的简单操作

    数据的操作 字符串的一些常用操作: 1 1 #!/usr/bin/env python 2 # #coding=utf-8 3 # 4 # test='hello world' 5 # print(t ...

  8. 【刷题】BZOJ 3140 [Hnoi2013]消毒

    Description 最近在生物实验室工作的小T遇到了大麻烦. 由于实验室最近升级的缘故,他的分格实验皿是一个长方体,其尺寸为abc,a.b.c 均为正整数.为了实验的方便,它被划分为abc个单位立 ...

  9. [luogu3806]【模板】点分治1

    description 求树上长度为\(k\)的路径是否存在. data range \[n\le 10000,k\le 10000000\] solution 点分治复习... 使用普通的点分治枚举 ...

  10. spring+springMVC+mybatis较全

    1.基本概念   1.1.Spring   Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson 在其著作Expert One-On- ...