阮一峰的IT笔记:首页 -> 分类 -> 数据库 查看所有文章:按分类 | 按月份

MySQL中的子查询(subqueries)

1.

示例:

select customerid, amount
from orders
where amount = (select max(amount) from orders);

2.

子查询中使用的操作符:

* ANY

SELECT c1 FROM t1 WHERE c1 > ANY (SELECT c1 FROM t2);

Returns true if the comparison is true for any of the rows in the subquery.

* IN

SELECT c1 FROM t1
WHERE c1 IN
(SELECT c1 from t2);

* SOME

SELECT c1 FROM t1
WHERE c1 >
SOME (SELECT c1 FROM t2);!

* ALL

SELECT c1 FROM t1
WHERE c1 >
ALL (SELECT c1 from t2);

3.

exists 操作符

select isbn, title
from books
where not exists
(select * from order_items where order_items.isbn=books.isbn);

4.

行匹配

select c1, c2, c3
from t1
where (c1, c2, c3) in (select c1, c2, c3 from t2);

5.

用子查询生成一个暂时性表格

select * from
(select customerid, name from customers where city=Box Hill)
as box_hill_customers;

注意,子查询后面的as从句是必须的。

« MySQL 笔记(III) | 首页 | MySQLi函数库 »

About

This page contains a single entry from the blog posted on 2007年11月13日 14:31.

The previous post in this blog was MySQL 笔记(III).

The next post in this blog is MySQLi函数库.

Many more can be found on the main index page or by looking through the archives.

Powered by
Movable Type 3.33

Post a comment