《Mysql入门详解MySQL中UNION的用法》要点:
本文介绍了Mysql入门详解MySQL中UNION的用法,希望对您有用。如果有疑问,可以联系我们。
如果想选择其他几个表中的行或从一个单一的表作为一个单独的成果集行的几个集会,那么可以使用的UNION.MYSQL学习
UNION在MySQL4.0以上版本能力可以使用.本节说明如何使用它.MYSQL学习
假设有两个表,潜在和实际的客户列表,供应商购买耗材合并所有三个表中的姓名和地址,来创建一个单一的邮件列表.UNION提供了一种办法做到这一点.假设三个表有以下内容:MYSQL学习
mysql> SELECT * FROM prospect; +---------+-------+------------------------+ | fname | lname | addr | +---------+-------+------------------------+ | Peter | Jones | 482 Rush St., Apt. 402 | | Bernice | Smith | 916 Maple Dr. | +---------+-------+------------------------+ mysql> SELECT * FROM customer; +-----------+------------+---------------------+ | last_name | first_name | address | +-----------+------------+---------------------+ | Peterson | Grace | 16055 Seminole Ave. | | Smith | Bernice | 916 Maple Dr. | | Brown | Walter | 8602 1st St. | +-----------+------------+---------------------+ mysql> SELECT * FROM vendor; +-------------------+---------------------+ | company | street | +-------------------+---------------------+ | ReddyParts, Inc. | 38 Industrial Blvd. | | Parts-to-go, Ltd. | 213B Commerce Park. | +-------------------+---------------------+
这不要紧,如果所有的三个表具有分歧的列名.下面的查询演示了如何选择一下子从三个表的名称和地址:MYSQL学习
mysql> SELECT fname, lname, addr FROM prospect -> UNION -> SELECT first_name, last_name, address FROM customer -> UNION -> SELECT company, '', street FROM vendor; +-------------------+----------+------------------------+ | fname | lname | addr | +-------------------+----------+------------------------+ | Peter | Jones | 482 Rush St., Apt. 402 | | Bernice | Smith | 916 Maple Dr. | | Grace | Peterson | 16055 Seminole Ave. | | Walter | Brown | 8602 1st St. | | ReddyParts, Inc. | | 38 Industrial Blvd. | | Parts-to-go, Ltd. | | 213B Commerce Park. | +-------------------+----------+------------------------+
如果想选择所有记录,包含重复的,请ALL的第一个UNION关键字:MYSQL学习
mysql> SELECT fname, lname, addr FROM prospect -> UNION ALL -> SELECT first_name, last_name, address FROM customer -> UNION -> SELECT company, '', street FROM vendor; +-------------------+----------+------------------------+ | fname | lname | addr | +-------------------+----------+------------------------+ | Peter | Jones | 482 Rush St., Apt. 402 | | Bernice | Smith | 916 Maple Dr. | | Grace | Peterson | 16055 Seminole Ave. | | Bernice | Smith | 916 Maple Dr. | | Walter | Brown | 8602 1st St. | | ReddyParts, Inc. | | 38 Industrial Blvd. | | Parts-to-go, Ltd. | | 213B Co妹妹erce Park. | +-------------------+----------+------------------------+
欢迎参与《Mysql入门详解MySQL中UNION的用法》讨论,分享您的想法,维易PHP学院为您提供专业教程。