1、mysql> SHOW COLUMNS FROM users;
+----------+----------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+----------------------+------+-----+---------+----------------+
| id | smallint(5) unsigned | NO | PRI | NULL | auto_increment |
| username | varchar(20) | NO | | NULL | |
| password | varchar(20) | NO | | NULL | |
| age | tinyint(3) unsigned | NO | | 10 | |
| sex | tinyint(1) | YES | | NULL | |
+----------+----------------------+------+-----+---------+----------------+
5 rows in set (0.01 sec)

mysql> SELECT id,username FROM users;

+----+----------+
| id | username |
+----+----------+
| 1 | Tom |
| 2 | John |
| 3 | Tom |
| 4 | John |
| 10 | John |
| 11 | ROSE |
| 12 | Ben |
+----+----------+
7 rows in set (0.00 sec)

mysql> SELECT username,id FROM users;

+----------+----+
| username | id |
+----------+----+
| Tom | 1 |
| John | 2 |
| Tom | 3 |
| John | 4 |
| John | 10 |
| ROSE | 11 |
| Ben | 12 |
+----------+----+

mysql> SELECT users.id, users.username FROM users;
+----+----------+
| id | username |
+----+----------+
| 1 | Tom |
| 2 | John |
| 3 | Tom |
| 4 | John |
| 10 | John |
| 11 | ROSE |
| 12 | Ben |
+----+----------+

mysql> SELECT id AS userID, username AS uname FROM users;
+--------+-------+
| userID | uname |
+--------+-------+
| 1 | Tom |
| 2 | John |
| 3 | Tom |
| 4 | John |
| 10 | John |
| 11 | ROSE |
| 12 | Ben |
+--------+-------+

2、查询结果分组

[GROUP BY {col_name | position} [ASC | DESC], ...]

mysql> SELECT * FROM users;
+----+----------+----------+-----+------+
| id | username | password | age | sex |
+----+----------+----------+-----+------+
| 1 | Tom | 123 | 29 | 0 |
| 2 | John | 456 | 38 | 0 |
| 3 | Tom | 123 | 27 | 0 |
| 4 | John | 456 | 27 | 0 |
| 10 | John | 456 | 21 | 0 |
| 11 | ROSE | 67 | 4 | 0 |
| 12 | Ben | 456 | 13 | NULL |
+----+----------+----------+-----+------+
7 rows in set (0.01 sec)

mysql> SELECT sex FROM users GROUP BY sex;
+------+
| sex    |
+------+
| NULL  |
| 0       |
+------+

mysql>
mysql> SELECT sex FROM users GROUP BY 1;  # 1表示select语句中第一个出现的字段
+------+
| sex |
+------+
| NULL |
| 0 |
+------+

3、having语句设置分组条件

mysql> SELECT sex,age FROM users GROUP BY 1 HAVING age > 35;
Empty set (0.00 sec)

4、对查询结果进行排序

mysql> SELECT * FROM users;
+----+----------+----------+-----+------+
| id | username | password | age | sex |
+----+----------+----------+-----+------+
| 1 | Tom | 123 | 29 | 0 |
| 2 | John | 456 | 38 | 0 |
| 3 | Tom | 123 | 27 | 0 |
| 4 | John | 456 | 27 | 0 |
| 10 | John | 456 | 21 | 0 |
| 11 | ROSE | 67 | 4 | 0 |
| 12 | Ben | 456 | 13 | NULL |
+----+----------+----------+-----+------+
7 rows in set (0.00 sec)

mysql> SELECT * FROM users ORDER BY id DESC;  #以降序方式进行排序

+----+----------+----------+-----+------+
| id | username | password | age | sex |
+----+----------+----------+-----+------+
| 12 | Ben | 456 | 13 | NULL |
| 11 | ROSE | 67 | 4 | 0 |
| 10 | John | 456 | 21 | 0 |
| 4 | John | 456 | 27 | 0 |
| 3 | Tom | 123 | 27 | 0 |
| 2 | John | 456 | 38 | 0 |
| 1 | Tom | 123 | 29 | 0 |
+----+----------+----------+-----+------+
7 rows in set (0.00 sec)

mysql> SELECT * FROM users | Tom | 123 | 27 | 0 |
| | John | 456 | 27 | 0 |
| 1 | Tom | 123 | 29 | 0 |
| 2 | John | 456 | 38 | 0 |
+----+----------+----------+-----+------+
7 rows in set (0.00 sec)

mysql> | John | 456 | 27 | 0 |
| | Tom | 123 | 27 | 0 |
| 1 | Tom | 123 | 29 | 0 |
| 2 | John | 456 | 38 | 0 |
+----+----------+----------+-----+------+

5、限制查询结果返回的数量

[LIMIT  {[OFFSET,] row_count | row_count OFFSET  offset}]

mysql> SELECT * FROM users;
+----+----------+----------+-----+------+
| id | username | password | age | sex |
+----+----------+----------+-----+------+
| 1 | Tom | 123 | 29 | 0 |
| 2 | John | 456 | 38 | 0 |
| 3 | Tom | 123 | 27 | 0 |
| 4 | John | 456 | 27 | 0 |
| 10 | John | 456 | 21 | 0 |
| 11 | ROSE | 67 | 4 | 0 |
| 12 | Ben | 456 | 13 | NULL |
+----+----------+----------+-----+------+
7 rows in set (0.00 sec)

mysql> SELECT * FROM users LIMIT 2;
+----+----------+----------+-----+------+
| id | username | password | age | sex |
+----+----------+----------+-----+------+
| 1 | Tom | 123 | 29 | 0 |
| 2 | John | 456 | 38 | 0 |
+----+----------+----------+-----+------+
2 rows in set (0.00 sec)

mysql> SELECT * FROM users LIMIT 2,2;  #第一个2为偏移量,第二个为限制为2条记录
+----+----------+----------+-----+------+
| id | username | password | age | sex |
+----+----------+----------+-----+------+
| 3 | Tom | 123 | 27 | 0 |
| 4 | John | 456 | 27 | 0 |
+----+----------+----------+-----+------+
2 rows in set (0.00 sec)

mysql>
mysql> SELECT * FROM users ORDER BY id desc;
+----+----------+----------+-----+------+
| id | username | password | age | sex |
+----+----------+----------+-----+------+
| 12 | Ben | 456 | 13 | NULL |
| 11 | ROSE | 67 | 4 | 0 |
| 10 | John | 456 | 21 | 0 |
| 4 | John | 456 | 27 | 0 |
| 3 | Tom | 123 | 27 | 0 |
| 2 | John | 456 | 38 | 0 |
| 1 | Tom | 123 | 29 | 0 |
+----+----------+----------+-----+------+
7 rows in set (0.00 sec)

mysql> SELECT * FROM users ORDER BY id desc LIMIT 2,2;
+----+----------+----------+-----+------+
| id | username | password | age | sex |
+----+----------+----------+-----+------+
| 10 | John | 456 | 21 | 0 |
| 4 | John | 456 | 27 | 0 |
+----+----------+----------+-----+------+
2 rows in set (0.01 sec)

mysql查询表达式解析的更多相关文章

  1. (五)lucene之特定项搜索和查询表达式

    需求:模糊搜索. 前提:  本例中使用lucene 5.3.0 package com.shyroke.lucene; import java.io.File; import java.io.File ...

  2. MySQL慢日志查询全解析:从参数、配置到分析工具【转】

    转自: MySQL慢日志查询全解析:从参数.配置到分析工具 - MySQL - DBAplus社群——围绕数据库.大数据.PaaS云,运维圈最专注围绕“数据”的学习交流和专业社群http://dbap ...

  3. Thinkphp中查询复杂sql查询表达式,如何表达MYSQL中的某字段不为空is not null?

    Thinkphp中查询复杂sql查询表达式,如何表达MYSQL中的某字段不为空is not null?先上两种实现方式的实例:$querys["house_type_image"] ...

  4. mysql查询相关的命令解析

    特:不重启mysql 更新配置文件方法(不允许重启mysql实例或连接不上msyql服务器): gdb -p $(pidof mysqld) -ex "set max_connections ...

  5. mysql查询性能优化

    mysql查询过程: 客户端发送查询请求. 服务器检查查询缓存,如果命中缓存,则返回结果,否则,继续执行. 服务器进行sql解析,预处理,再由优化器生成执行计划. Mysql调用存储引擎API执行优化 ...

  6. 如何使用 Entity Framework 构造动态查询表达式

    一般的程序员做上几年以后, 或多或少的都有些代码的积累, 我也不例外. 作为微软技术程序员, 自从Linq和EF出来之后, 就基本上爱不释手了, 且不说执行效率的问题, 单单就开发效率和代码的可移植性 ...

  7. MySQL查询执行过程

    MySQL查询执行路径 1. 客户端发送一条查询给服务器: 2. 服务器先会检查查询缓存,如果命中了缓存,则立即返回存储在缓存中的结果.否则进入下一阶段: 3. 服务器端进行SQL解析.预处理,再由优 ...

  8. MySQL查询性能优化(精)

    MySQL查询性能优化 MySQL查询性能的优化涉及多个方面,其中包括库表结构.建立合理的索引.设计合理的查询.库表结构包括如何设计表之间的关联.表字段的数据类型等.这需要依据具体的场景进行设计.如下 ...

  9. MySQL查询不使用索引汇总 + 如何优化sql语句

    不使用索引原文 : http://itlab.idcquan.com/linux/MYSQL/918330.html MySQL查询不使用索引汇总 众所周知,增加索引是提高查询速度的有效途径,但是很多 ...

随机推荐

  1. nginx+腾讯云免费ssl证书+阿里云ECS实现Https配置

    1. 申请SSL证书: 首先我们需要到腾讯云那边申请一个ssl证书,对于个人博客类型的,建议采用免费版本,土豪除外 申请地址:https://console.cloud.tencent.com/ssl ...

  2. Gym - 100989L

    After the data structures exam, students lined up in the cafeteria to have a drink and chat about ho ...

  3. string的基本用法

    #include <iostream> #include<string> #include<vector> #include<algorithm> us ...

  4. 通过jpa getResultList 获取查询到的内容

    String sql = "select * from spatial_event "; Query query = em.createNativeQuery(sql); // q ...

  5. 你应该要知道的vue.js

    前言 小组同事最近都在学习前端,目前我们小组前端技术栈主要是vue.在和同事交流过程成,发现他们对vue都不了解,所以整理了问的比较多的问题. 组件data为什么必须是函数? 因为组件可能被多处使用, ...

  6. 解决在vue中axios请求超时的问题

    查看更多精彩内容请访问我的新博客:https://www.cssge.com/ 自从使用Vue2之后,就使用官方推荐的axios的插件来调用API,在使用过程中,如果服务器或者网络不稳定掉包了, 你们 ...

  7. JAVA概述 也许你会豁然开朗

    1.JDK:Java Development Kit,java的开发和运行环境,java的开发工具和jre. 2.JRE:Java Runtime Environment,java程序的运行环境,ja ...

  8. easyUI,重新渲染

    Easyui中使用jquery或js动态添加元素时出现的样式失效的解决方法 可以使用$.parser.parse();这个方法进行处理: 例如: $.parser.parse(); 表示对整个页面重新 ...

  9. 反汇编Dis解析

    目录 反汇编dis解析 COMM段BSS段 注释段 Bl指令 title: 反汇编Dis解析 tags: ARM date: 2018-10-21 18:02:58 --- 反汇编dis解析 关于段, ...

  10. proxyServer Squid 3.5.5 / 20181111

    s 参考资料1 四次握手用来关闭已建立的TCP连接 1. (B) –> ACK/FIN –> (A) 2. (B) <– ACK <– (A) 3. (B) <– ACK ...