`
flyfox1982
  • 浏览: 78987 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

mysql之any,some all

 
阅读更多

ALL、ANY和SOME子查询

ALL和ANY操作符的常见用法是结合一个相对比较操作符对一个数据列子查询的结果进行测试。它们测试比较值是否与子查询所返回的全部或一部分值匹配。比如说,如果比较值小于或等于子查询所返回的每一个值,<= ALL将是true;只要比较值小于或等于子查询所返回的任何一个值,<= ANY将是true。SOME是ANY的一个同义词。

下面这条语句用来检索最早出生的总统,具体做法是选取出生日期小于或等于president数据表里的所有出生日期(只有最早的出生日期满足这一条件)的那个数据行:

  1. mysql>SELECTlast_name,first_name,birthFROMpresident
  2. ->WHEREbirth<=ALL(SELECTbirthFROMpresident);
  3. +------------+------------+------------+
  4. |last_name|first_name|birth|
  5. +------------+------------+------------+
  6. |Washington|George|1732-02-22|
  7. +------------+------------+------------+

下面这条语句的用处就不大了,它将返回所有的数据行,因为对于每个日期,至少有一个日期(它本身)大于或等于它:

  1. mysql>SELECTlast_name,first_name,birthFROMpresident
  2. ->WHEREbirth<=ANY(SELECTbirthFROMpresident);
  3. +------------+---------------+------------+
  4. |last_name|first_name|birth|
  5. +------------+---------------+------------+
  6. |Washington|George|1732-02-22|
  7. |Adams|John|1735-10-30|
  8. |Jefferson|Thomas|1743-04-13|
  9. |Madison|James|1751-03-16|
  10. |Monroe|James|1758-04-28|
  11. ...

当ALL、ANY或SOME操作符与"="比较操作符配合使用时,子查询可以是一个数据表子查询。此时,你需要使用一个数据行构造器来提供与子查询所返回的数据行进行比较的比较值。

  1. mysql>SELECTlast_name,first_name,city,stateFROMpresident
  2. ->WHERE(city,state)=ANY
  3. ->(SELECTcity,stateFROMpresident
  4. ->WHERElast_name='Roosevelt');
  5. +-----------+-------------+-----------+-------+
  6. |last_name|first_name|city|state|
  7. +-----------+-------------+-----------+-------+
  8. |Roosevelt|Theodore|NewYork|NY|
  9. |Roosevelt|FranklinD.|HydePark|NY|
  10. +-----------+-------------+-----------+-------+

前一节里提到过,IN和NOT IN操作符是= ANY和< > ALL的简写。也就是说,IN操作符的含义是"等于子查询所返回的某个数据行",NOT IN操作符的含义是"不等于子查询所返回的任何数据行"。

分享到:
评论

相关推荐

    mysql 5.1.59

    Some Reference Manual sections of special interest: - If you are migrating from an older version of MySQL, please read the "Upgrading from..." section. - To see what MySQL can do, take a look at the...

    MySQL 8 Administrator’s Guide

    MySQL is one of the most popular ...By the end of this highly practical book, you will have all the knowledge you need to tackle any problem you might encounter while administering your MySQL solution.

    A Guide for Migrating From Oracle to MySQL

    savings and feature set of MySQL make for a compelling business case to offload some or all their database-driven applications to the MySQL database server. This paper provides insight into what is ...

    NBU mysql备份管理

    Some of the Third Party Programs are available under open source or free software licenses. The License Agreement accompanying the Software does not alter any rights or obligations you may have under...

    MySQL 子查询(subquery)语法与用法实例.docx

    此外,MySQL 子查询还支持其他类型的操作符,例如 EXISTS、ANY、SOME、ALL 等。这些操作符可以根据实际情况选择使用。 在 MySQL 中,子查询可以在 SELECT、FROM、WHERE、HAVING 等子句中使用。例如 `SELECT * FROM ...

    MySQL命令大全

    MYSQL常用命令 1.导出整个数据库 mysqldump -u 用名 -p –default-character-set=latin1 数据库名 &gt; 导出的文件名(数据库默认编码是latin1) mysqldump -u wcnc -p smgp_apps_wcnc &gt; wcnc.sql 2.导出一个表 ...

    PHP and MySQL by Example.pdf

    Any remaining mistakes are my own. I’d also like to thank the students in my classes who provided valuable input for the labs. These include Rita McCue, Sanjay Shahri, Ryan Belcher, Debra ...

    cvs2mysql

    This class creates sql to import into tables in any mysql table - you specify the fieldname in the csv - and the mysql field in your db that this relates to. It supports database queries to get ...

    MYSQL常用命令大全

    MYSQL常用命令 1.导出整个数据库 mysqldump -u 用户名 -p --default-character-set=latin1 数据库名 &gt; 导出的文件名(数据库默认编码是latin1) mysqldump -u wcnc -p smgp_apps_wcnc &gt; wcnc.sql 2.导出一个表 ...

    MySQL中列子查询与行子查询操作的学习教程

    MySQL 列子查询及 IN、ANY、SOME 和 ALL 操作符的使用 MySQL 列子查询 列子查询是指子查询返回的结果集是 N 行一列,该结果通常来自对表的某个字段查询返回。 一个列子查询的例子如下: SELECT * FROM article ...

    MySQL子查询的几种常见形式介绍

    mysql子查询的几种常见写法: 代码如下:select * from xxx where col = [any|all](select * from xxxx); 该句法可分为加关键词和不加关键词的写法,当不加关键词的时候,子查询语句返回的是一个离散值(注意是一个)...

    MySql基本查询、连接查询、子查询、正则表达查询讲解

    MySQL中是使用SELECT语句来查询数据的。在这一章中将讲解的内容包括。 1、查询语句的基本语法 2、在单表上查询数据 3、使用聚合函数查询数据 4、多表上联合查询 5、子查询 6、合并查询结果 7、为表和字段取别名 8、...

    Mastering The Faster Web with PHP, MySQL, and JavaScript 1st pdf

    After reading this book, you will know how to boost the performance of any Web application and make it part of what has come to be known as the Faster Web. What you will learn Install, confgure, and...

    基于mysql查询语句的使用详解

     但是运行的时候会报 This version of MySQL doesn’t yet support ‘LIMIT & IN/ALL/ANY/SOME subquery 这个的意思是表示子查询的时候不支持limit ,还有一点我就是很二了 就是查询的时候用not in 效率非常不高 ...

    PHP and MySQL Web Devepopment 4th Edition.pdf

    The /E switch is similar to the /A switch, except that instead of telling DCDIAG to test all of the domain controllers in the current site, it tells DCDIAG to test every domain controller in the ...

    Node.Patterns.Databases.Volume.I.LevelDB.Redis.and.CouchDB

    most of the existing Node web frameworks (like Express, Hapi and others) don't impose any database or even any type of database at all. This bring-your-own-database approach has been in part fed by ...

    数据库课本例题(数据查询)

    3.嵌套查询(带有IN谓词的子查询、带有比较运算符的子查询、带有ANY(SOME)或ALL谓词的子查询、带有EXISTS谓词的子查询) 4、集合查询(并操作UNION、交操作INTERSECT、差操作EXCEPT) 5.基于派生表的查询

    端口查看工具

    CurrPorts displays the list of all currently opened TCP/IP and UDP ports on your local computer. For each port in the list, information about the process that opened the port is also displayed, ...

    Begining SQL

    SQL is an international standard for manipulating data in databases and is used by database programmers in all major database systems: Microsoft, IBM, Oracle, MySQL, and many others From the Back ...

Global site tag (gtag.js) - Google Analytics