1,select查询的基本结构

select 字段
from 表
where 过滤条件
group by 分组条件 having 过滤的第二条件
order by 排序条件
limit 限定结果条件;

  

2,最简单的查询语句

mysql> select * from user;
+----+----------+----------+---------------------+
| id | username | password | createtime |
+----+----------+----------+---------------------+
| 1 | kenan | kenan | 2012-10-31 15:32:26 |
| 3 | kenan | lele | 2012-10-31 15:32:26 |
+----+----------+----------+---------------------+

3,查询一列或多列

mysql> select username,createtime from user;
+----------+---------------------+
| username | createtime |
+----------+---------------------+
| kenan | 2012-10-31 15:32:26 |
| kenan | 2012-10-31 15:32:26 |
+----------+---------------------+
2 rows in set (0.00 sec)

4,多表查询

mysql> select * from user;
+----+----------+----------+---------------------+
| id | username | password | createtime |
+----+----------+----------+---------------------+
| 1 | kenan | kenan | 2012-10-31 15:32:26 |
| 3 | kenan | lele | 2012-10-31 15:32:26 |
+----+----------+----------+---------------------+
2 rows in set (0.00 sec) mysql> select * from teacher;
+----+------------+---------------------+
| id | username | createtime |
+----+------------+---------------------+
| 1 | Teacher Li | 2012-11-01 21:00:58 |
| 2 | Teacher Le | 2012-11-01 21:02:07 |
+----+------------+---------------------+
2 rows in set (0.00 sec) mysql> select * from user,teacher;
+----+----------+----------+---------------------+----+------------+---------------------+
| id | username | password | createtime | id | username | createtime |
+----+----------+----------+---------------------+----+------------+---------------------+
| 1 | kenan | kenan | 2012-10-31 15:32:26 | 1 | Teacher Li | 2012-11-01 21:00:58 |
| 3 | kenan | lele | 2012-10-31 15:32:26 | 1 | Teacher Li | 2012-11-01 21:00:58 |
| 1 | kenan | kenan | 2012-10-31 15:32:26 | 2 | Teacher Le | 2012-11-01 21:02:07 |
| 3 | kenan | lele | 2012-10-31 15:32:26 | 2 | Teacher Le | 2012-11-01 21:02:07 |
+----+----------+----------+---------------------+----+------------+---------------------+
4 rows in set (0.00 sec) 多表查询中,如果没有限制条件的话,两个表的记录会分别进行匹配,产生的结果就是两个表的笛卡尔积,叫做全连接
下面的这个带有条件的叫做等同连接,相当于在上面的查询结果中挑选出来满足where后面条件的记录,因为没有所有是空的结果。

  

mysql> select * from user,teacher where user.username = teacher.username;
Empty set (0.00 sec)

  

5,带有简单的where字句条件查询

mysql> select * from user where id>2;
+----+----------+----------+---------------------+
| id | username | password | createtime |
+----+----------+----------+---------------------+
| 3 | kenan | lele | 2012-10-31 15:32:26 |
+----+----------+----------+---------------------+
1 row in set (0.00 sec)

  

6,带有in的查询,in用来表示范围

mysql> select * from user where username in ('kenan','Micheal');
+----+----------+----------+---------------------+
| id | username | password | createtime |
+----+----------+----------+---------------------+
| 1 | kenan | kenan | 2012-10-31 15:32:26 |
| 3 | kenan | lele | 2012-10-31 15:32:26 |
+----+----------+----------+---------------------+
2 rows in set (0.00 sec)
这里查询出来username是kenan或者是Micheal的记录

7,between and 用来表示一个字段的范围的查询

mysql> select * from user where id between 2 and 3;
+----+----------+----------+---------------------+
| id | username | password | createtime |
+----+----------+----------+---------------------+
| 3 | kenan | lele | 2012-10-31 15:32:26 |
+----+----------+----------+---------------------+
1 row in set (0.00 sec) mysql> select * from user where id between 3 and 4;
+----+----------+----------+---------------------+
| id | username | password | createtime |
+----+----------+----------+---------------------+
| 3 | kenan | lele | 2012-10-31 15:32:26 |
+----+----------+----------+---------------------+
1 row in set (0.00 sec)
这里分别查询出来id在2和3之间的记录,3到4之间的记录,从结果可以看出 between and是包含边界的,就是>=2而且<=3

8, is null 查询空值

mysql> select * from user ;
+----+----------+----------+---------------------+
| id | username | password | createtime |
+----+----------+----------+---------------------+
| 1 | kenan | kenan | 2012-10-31 15:32:26 |
| 3 | kenan | lele | 2012-10-31 15:32:26 |
| 4 | lele | lele | NULL |
+----+----------+----------+---------------------+
3 rows in set (0.00 sec) mysql> select * from user where createtime is null;
+----+----------+----------+------------+
| id | username | password | createtime |
+----+----------+----------+------------+
| 4 | lele | lele | NULL |
+----+----------+----------+------------+
1 row in set (0.00 sec)

  

9,用and连接多条件查询  并关系

mysql> select * from user where username = 'kenan' and password = 'kenan';
+----+----------+----------+---------------------+
| id | username | password | createtime |
+----+----------+----------+---------------------+
| 1 | kenan | kenan | 2012-10-31 15:32:26 |
+----+----------+----------+---------------------+
1 row in set (0.00 sec)

  

10,用or 的多条件查询  这个是或关系,满足一个条件即可

mysql> select * from user where username = 'kenan' or password = 'kenan';
+----+----------+----------+---------------------+
| id | username | password | createtime |
+----+----------+----------+---------------------+
| 1 | kenan | kenan | 2012-10-31 15:32:26 |
| 3 | kenan | lele | 2012-10-31 15:32:26 |
+----+----------+----------+---------------------+
2 rows in set (0.00 sec)

  

11,聚合函数  count()用来统计总数

mysql> select count(*) from user;
+----------+
| count(*) |
+----------+
| 3 |
+----------+
1 row in set (0.00 sec)

  

12,使用sum()求和,使用avg函数求平均值

mysql> select id from user;
+----+
| id |
+----+
| 1 |
| 3 |
| 4 |
+----+
3 rows in set (0.00 sec) mysql> select sum(id) from user;
+---------+
| sum(id) |
+---------+
| 8 |
+---------+
1 row in set (0.00 sec) mysql> select avg(id) from user;
+---------+
| avg(id) |
+---------+
| 2.6667 |
+---------+
1 row in set (0.04 sec)

13,使用max()求最大值,使用min求最小值

mysql> select id from user;
+----+
| id |
+----+
| 1 |
| 3 |
| 4 |
+----+
3 rows in set (0.00 sec) mysql> select max(id) from user;
+---------+
| max(id) |
+---------+
| 4 |
+---------+
1 row in set (0.00 sec) mysql> select min(id) from user;
+---------+
| min(id) |
+---------+
| 1 |
+---------+
1 row in set (0.00 sec)

原文链接:http://soukenan.blog.51cto.com/5130995/1047201

PHP之路——MySql查询语句的更多相关文章

  1. MySQL查询语句执行过程及性能优化(JOIN/ORDER BY)-图

    http://blog.csdn.net/iefreer/article/details/12622097 MySQL查询语句执行过程及性能优化-查询过程及优化方法(JOIN/ORDER BY) 标签 ...

  2. mysql查询语句,通过limit来限制查询的行数。

    mysql查询语句,通过limit来限制查询的行数. 例如: select name from usertb where age > 20 limit 0, 1; //限制从第一条开始,显示1条 ...

  3. MYSQL查询语句大全集锦

    MYSQL查询语句大全集锦 1:使用SHOW语句找出在服务器上当前存在什么数据库: mysql> SHOW DATABASES; 2:2.创建一个数据库MYSQLDATA mysql> C ...

  4. MySQL查询语句执行过程及性能优化-基本概念和EXPLAIN语句简介

    网站或服务的性能关键点很大程度在于数据库的设计(假设你选择了合适的语言开发框架)以及如何查询数据上. 我们知道MySQL的性能优化方法,一般有建立索引.规避复杂联合查询.设置冗余字段.建立中间表.查询 ...

  5. MySQL查询语句执行过程及性能优化-查询过程及优化方法(JOIN/ORDER BY)

    在上一篇文章MySQL查询语句执行过程及性能优化-基本概念和EXPLAIN语句简介中介绍了EXPLAIN语句,并举了一个慢查询例子:

  6. mysql查询语句集

    1. mysql 查询出某字段的值不为空的语句 1.不为空 select * from table where id <> ""; select * from tabl ...

  7. [转]MySQL查询语句执行过程详解

    Mysql查询语句执行原理 数据库查询语句如何执行?语法分析:首先进行语法分析,对使用sql表示的查询进行语法分析,生成查询语法分析树.语义检查:检查sql中所涉及的对象以及是否在数据库中存在,用户是 ...

  8. Mysql查询语句中字符型字段不区分大小写解决方法

    项目中和前端联调的时候,发现Mysql查询语句中字符型字段值过滤是不区分大小写的,之前没有关注过这个设置,特意去网上看了下,原因是Mysql中“COLLATE”属性区分大小写,而该属性默认值为“utf ...

  9. php面试专题---MYSQL查询语句优化

    php面试专题---MYSQL查询语句优化 一.总结 一句话总结: mysql的性能优化包罗甚广: 索引优化,查询优化,查询缓存,服务器设置优化,操作系统和硬件优化,应用层面优化(web服务器,缓存) ...

随机推荐

  1. CLOSE-UP FORMALWEAR_意大利进口_2015秋冬_男装发布会_西装图片系列_男装西装设计资料_WeArTrends时尚资讯网_国内最专业的服装设计资讯网站

    CLOSE-UP FORMALWEAR_意大利进口_2015秋冬_男装发布会_西装图片系列_男装西装设计资料_WeArTrends时尚资讯网_国内最专业的服装设计资讯网站 CLOSE-UP FORMA ...

  2. [转载]10 Best Tools For Websites And Apps Development Ever

    转载自: http://www.websurfmedia.com/10-best-tools-for-websites-and-apps-development-ever/   The world i ...

  3. Hibernate五 HQL查询

    HQL查询一 介绍1.HQL:Hibernate Query Language,是一种完全面向对象的查询语言.使用Hibernate有多重查询方式可供选择:hibernate的HQL查询,也可以使用条 ...

  4. Gridview实现银行选择列表

    [MainActivity.java] package com.example.activitydemo; import android.os.Bundle; import android.view. ...

  5. springMVC3学习(二)--ModelAndView对象

    当控制器处理完请求时,一般会将包括视图名称或视图对象以及一些模型属性的ModelAndView对象返回到DispatcherServlet. 因此,常常须要在控制器中构造ModelAndView对象. ...

  6. 那些年不错的Android开源项目(转)

    第一部分 个性化控件(View) 主要介绍那些不错个性化的View,包括ListView.ActionBar.Menu.ViewPager.Gallery.GridView.ImageView.Pro ...

  7. 关于Daydream VR的最直白的介绍

    虚拟现实(Virtual Reality),简称虚拟技术,也称虚拟环境,是利用电脑模拟产生一个三度空间的虚拟世界,提供用户关于视觉等感官的模拟,让用户如同身历其境一般,电脑可以立即进行复杂的运算,将精 ...

  8. JSON 解析(门店)

    package com.j1.mai.action; import java.io.BufferedReader; import java.io.IOException; import java.io ...

  9. 【转】vue基础学习

    1.基本绑定:    new Vue(        {            el:'#elID',            data:{                // data obj     ...

  10. 正则表达式中/i,/g,/m的作用

    一./i (ignorCase)忽略大小写,注意仅是忽略大小写,并不忽略全半角. 二./g (globle)全文查找出现的所有匹配字符 三./m 1.(mutiple)多行查找2.m 影响 ^.$.3 ...