distinct 不显示重复的查询结果

(1) 对于表中一些没有唯一性约束的字段,可能存在重复的值,这时可以使用distinct来消除那些查询结果中的重复值

select  cust_id  from orders;
+---------+
| cust_id |
+---------+
| 10001 |
| 10001 |
| 10003 |
| 10004 |
| 10005 |
+---------+ select distinct cust_id from orders;
+---------+
| cust_id |
+---------+
| 10001 |
| 10003 |
| 10004 |
| 10005 |
+---------+

order by 查询结果排序

(1) order by 属性名 [desc|asc]
      这个语句的意义是:对于查询结果,指定根据这个属性来进行升序|降序的排列
(2) 排序时对于字段中的NULL值:
     asc升序时,NULL在最前面
     desc降序时,NULL在最后面

select distinct cust_id from orders order by cust_id desc;
+---------+
| cust_id |
+---------+
| 10005 |
| 10004 |
| 10003 |
| 10001 |
+---------+

limit 限制查询结果数目

(1) limit n:     表示显示前n条记录
(2) limit n,m: 表示从第n位置的记录开始,往后显示m条(注意:第一条记录的位置是0,往后依次类推)

select * from orders where order_num>2006;
+-----------+---------------------+---------+
| order_num | order_date | cust_id |
+-----------+---------------------+---------+
| 20005 | 2005-09-01 00:00:00 | 10001 |
| 20006 | 2005-09-12 00:00:00 | 10003 |
| 20007 | 2005-09-30 00:00:00 | 10004 |
| 20008 | 2005-10-03 00:00:00 | 10005 |
| 20009 | 2005-10-08 00:00:00 | 10001 |
+-----------+---------------------+---------+ select * from orders where order_num>2006 limit 2;
+-----------+---------------------+---------+
| order_num | order_date | cust_id |
+-----------+---------------------+---------+
| 20005 | 2005-09-01 00:00:00 | 10001 |
| 20006 | 2005-09-12 00:00:00 | 10003 |
+-----------+---------------------+---------+
select * from orders where order_num>2006 limit 0,2;
+-----------+---------------------+---------+
| order_num | order_date | cust_id |
+-----------+---------------------+---------+
| 20005 | 2005-09-01 00:00:00 | 10001 |
| 20006 | 2005-09-12 00:00:00 | 10003 |
+-----------+---------------------+---------+
select * from orders where order_num>2006 limit 2,2;
+-----------+---------------------+---------+
| order_num | order_date | cust_id |
+-----------+---------------------+---------+
| 20007 | 2005-09-30 00:00:00 | 10004 |
| 20008 | 2005-10-03 00:00:00 | 10005 |
+-----------+---------------------+---------+

union合并查询结果

(1) union all: 把所有查询结果合并

(2) union     : 把所有查询结果合并且去除重复行

select vend_id from vendors ;
+---------+
| vend_id |
+---------+
| 1001 |
| 1002 |
| 1003 |
| 1004 |
| 1005 |
| 1006 |
+---------+ select vend_id from products;
+---------+
| vend_id |
+---------+
| 1001 |
| 1001 |
| 1001 |
| 1002 |
| 1002 |
| 1003 |
| 1003 |
| 1003 |
| 1003 |
| 1003 |
| 1003 |
| 1003 |
| 1005 |
| 1005 |
+---------+ select vend_id from vendors union all select vend_id from products;
+---------+
| vend_id |
+---------+
| 1001 |
| 1002 |
| 1003 |
| 1004 |
| 1005 |
| 1006 |
| 1001 |
| 1001 |
| 1001 |
| 1002 |
| 1002 |
| 1003 |
| 1003 |
| 1003 |
| 1003 |
| 1003 |
| 1003 |
| 1003 |
| 1005 |
| 1005 |
+---------+ select vend_id from vendors union select vend_id from products;
+---------+
| vend_id |
+---------+
| 1001 |
| 1002 |
| 1003 |
| 1004 |
| 1005 |
| 1006 |
+---------+

[Mysql 查询语句]——对查询结果进一步的操作的更多相关文章

  1. T-SQL:SQL Server-数据库查询语句基本查询

    ylbtech-SQL Server-Basic:SQL Server-数据库查询语句基本查询 SQL Server 数据库查询语句基本查询. 1,数据库查询语句基本查询   数据库 SQL Serv ...

  2. SQL查询语句,怎样查询重复数据

    SQL查询语句,怎样查询重复数据 2012-02-22 14:50神奇的马骁勇 | 浏览 0 次 有表A, 中有字段id, name, memo现在有很多id重复的数据,怎么把这些重复的都查出来?gr ...

  3. mysql查询语句select-子查询

    1 子查询定义 在一个表表达中可以调用另一个表表达式,这个被调用的表表达式叫做子查询(subquery),我么也称作子选择(subselect)或内嵌选择(inner select).子查询的结果传递 ...

  4. 这里我们介绍的是 40+ 个非常有用的 Oracle 查询语句,主要涵盖了日期操作,获取服务器信息,获取执行状态,计算数据库大小等等方面的查询。这些是所有 Oracle 开发者都必备的技能,所以快快收藏吧!

    日期/时间 相关查询 获取当前月份的第一天 运行这个命令能快速返回当前月份的第一天.你可以用任何的日期值替换 “SYSDATE”来指定查询的日期. SELECT TRUNC (SYSDATE, 'MO ...

  5. [MySQL]子语句的查询技巧

    一.统计group by语句的行数 group by语句中,如果包含字段统计函数(诸如:count(),sum()...),这种情况下统计函数只会作用于group by的字段,因此想拿到最终结果的行数 ...

  6. MySQL数据库 : 查询语句,连接查询及外键约束

    查询指定字段        select 字段1,字段2 from 表名; 消除重复行(重复指的是结果集中的所有完全重复行)             select distinct 字段1,字段2.. ...

  7. mysql sql语句执行时间查询

    第一种:show profiles 之类的语句来查看 1.查一下profile是不是打开了,默认是不打开的. mysql> show profiles; Empty set (0.02 sec) ...

  8. [Mysql 查询语句]——分组查询group by

    group by (1) group by的含义:将查询结果按照1个或多个字段进行分组,字段值相同的为一组(2) group by可用于单个字段分组,也可用于多个字段分组 select * from ...

  9. Mysql 基本语句 + 高级查询

    MySQL执行SQL脚本文件的命令: 从cmd进入mysql命令行模式: mysql> -uroot –prootpassword –Ddatabasename 如果是我本地的数据库,就相应修改 ...

随机推荐

  1. 手动安装httpd服务器

    首先安装apr(Apache Portable Runtime) apr-util apr-iconv 安装之前需要 前置知识: 自己手动编译安装的软件的安装位置: /usr/local bin, s ...

  2. oracle数据导出导入(exp/imp)

    1.本地数据库导入导出 1.导出 (运行---cmd中操作)exp 用户名/密码@数据库实例名file=本地存放路径eg: exp jnjp/jnjp@ORCL file=C:/jnjp.dmp 2. ...

  3. 一起学习《C#高级编程》2--比较对象的相等性

    今后争取每两天能更新一次.平日的诱惑太多,双休只顾玩了,进度有点慢. 接上一讲的,类型的安全性,留下了点小尾巴——比较对象的相等性. C#有四种比较相等的方式:除了“==”运算符外,System.Ob ...

  4. C#基础笔记(第十七天)

    1.复习 ref 传地址 用的是同一块内存 一个改变另一个也随着改变 return n1 > n2 ? n1 : n2; 三元表达式 int max=GetMax(1,2,3,4,5,6,); ...

  5. 'Install app for SharePoint': Sideloading of apps is not enabled on this site

    http://blog.lekman.com/2012/11/sharepoint-2013-sideloading-of-apps-is.html Solution: You need to ena ...

  6. Mysql 中日期类型bigint和datetime互转

    MySql数据库中字段类型bigint 长度是10位的 mysql> select (from_unixtime(1554047999))as datatime;+--------------- ...

  7. GO学习笔记 - 基本数据类型

    官方教程:https://tour.go-zh.org/basics/11 Go 的基本类型有Basic types bool string int int8 int16 int32 int64 ui ...

  8. C#读入整数

    // ClassLibrary1.h #include<iostream> #pragma once using namespace System; namespace ClassLibr ...

  9. python 杂谈

    python 当前文件导入自定义模块的时候,会默认执行一遍 python使用的变量必须是已经定义或者声明过的.

  10. “全栈2019”Java异常第十一章:重写方法时只能抛出父类异常子集

    难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java异 ...