mysql查询表达式解析
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查询表达式解析的更多相关文章
- (五)lucene之特定项搜索和查询表达式
需求:模糊搜索. 前提: 本例中使用lucene 5.3.0 package com.shyroke.lucene; import java.io.File; import java.io.File ...
- MySQL慢日志查询全解析:从参数、配置到分析工具【转】
转自: MySQL慢日志查询全解析:从参数.配置到分析工具 - MySQL - DBAplus社群——围绕数据库.大数据.PaaS云,运维圈最专注围绕“数据”的学习交流和专业社群http://dbap ...
- Thinkphp中查询复杂sql查询表达式,如何表达MYSQL中的某字段不为空is not null?
Thinkphp中查询复杂sql查询表达式,如何表达MYSQL中的某字段不为空is not null?先上两种实现方式的实例:$querys["house_type_image"] ...
- mysql查询相关的命令解析
特:不重启mysql 更新配置文件方法(不允许重启mysql实例或连接不上msyql服务器): gdb -p $(pidof mysqld) -ex "set max_connections ...
- mysql查询性能优化
mysql查询过程: 客户端发送查询请求. 服务器检查查询缓存,如果命中缓存,则返回结果,否则,继续执行. 服务器进行sql解析,预处理,再由优化器生成执行计划. Mysql调用存储引擎API执行优化 ...
- 如何使用 Entity Framework 构造动态查询表达式
一般的程序员做上几年以后, 或多或少的都有些代码的积累, 我也不例外. 作为微软技术程序员, 自从Linq和EF出来之后, 就基本上爱不释手了, 且不说执行效率的问题, 单单就开发效率和代码的可移植性 ...
- MySQL查询执行过程
MySQL查询执行路径 1. 客户端发送一条查询给服务器: 2. 服务器先会检查查询缓存,如果命中了缓存,则立即返回存储在缓存中的结果.否则进入下一阶段: 3. 服务器端进行SQL解析.预处理,再由优 ...
- MySQL查询性能优化(精)
MySQL查询性能优化 MySQL查询性能的优化涉及多个方面,其中包括库表结构.建立合理的索引.设计合理的查询.库表结构包括如何设计表之间的关联.表字段的数据类型等.这需要依据具体的场景进行设计.如下 ...
- MySQL查询不使用索引汇总 + 如何优化sql语句
不使用索引原文 : http://itlab.idcquan.com/linux/MYSQL/918330.html MySQL查询不使用索引汇总 众所周知,增加索引是提高查询速度的有效途径,但是很多 ...
随机推荐
- debugger
今天爬取cfda时遇到的困难,一旦开启了调试,就debugger pause, ???还有这种操作 一顿google,百度,解决了这个问题,点一下Deactivate breakPoints,然后点一 ...
- QML学习笔记(四)-TabView-竖直方向
源码:https://github.com/sueRimn/QML-ExampleDemos 作者: 狐狸家的鱼 Github: 八至 版权声明:如需转载请获取授权和联系作者 想实现垂直竖直方向的Ta ...
- bzoj2554: Color
Description 有n个球排成一列,每个球都有一个颜色,用A-Z的大写字母来表示,我们每次随机选出两个球ball1,ball2,使得后者染上前者的颜色,求期望操作多少次,才能使得所有球的颜色都一 ...
- JavaScript原型详解
1,前言 下面是2008年Github创建以来,各种编程语言的排名情况 排名其中JavaScript自2015年之后就盘踞第一名,成为github上被使用最多的语言,早期,JS的使用还主要集中于浏览器 ...
- eclipse导出svn源码,如何转化为项目
1.先导出 2.点击项目右键,选“属性” 3.选择project facets 4.添加对应的支持 5.可进行进一步配置,设置name,然后点击确定等待完成
- BZOJ2006 ST表 + 堆
https://www.lydsy.com/JudgeOnline/problem.php?id=2006 题意:在长度N的序列中求K段长度在L到R之间的区间,使得他们的和最大 很容易想到要求一个前缀 ...
- bzoj2004 矩阵快速幂优化状压dp
https://www.lydsy.com/JudgeOnline/problem.php?id=2004 以前只会状压dp和矩阵快速幂dp,没想到一道题还能组合起来一起用,算法竞赛真是奥妙重重 小Z ...
- Java引用类型传递整理
引用数据类型(类) 10.1引用数据类型分类 可以把类的类型为两种: Java为我们提供好的类,如Scanner类,Random类等,这些已存在的类中包含了很多的方法与属性,可供我们使用. 我们自己创 ...
- python OrderDict
# encoding: utf-8 import csv import collections d = {'banana':3,'apple':4,'pear':1,'orange':2} print ...
- nginx变量(日志log_format)
nginx变量(日志log_format) HTTP请求变量 - arg_PARAMETER.http_HEADER.sent_http_HEADER 它是指http请求中的变量,举例: curl访问 ...