一、 联合查询 UNION, INTERSECT, EXCEPT
UNION运算符可以将两个或两个以上Select语句的查询结果集合合并成一个结果集合显示,即执行联合查询。UNION的语法格式为:
select_statement UNION [ALL] selectstatement [UNION [ALL] selectstatement][…n]
其中selectstatement为待联合的Select查询语句。
ALL选项表示将所有行合并到结果集合中。不指定该项时,被联合查询结果集合中的重复行将只保留一行。
联合查询时,查询结果的列标题为第一个查询语句的列标题。因此,要定义列标题必须在第一个查询语句中定义。要对联合查询结果排序时,也必须使用第一查询语句中的列名、列标题或者列序号。 在使用UNION 运算符时,应保证每个联合查询语句的选择列表中有相同数量的表达式,并且每个查询选择表达式应具有相同的数据类型,或是可以自动将它们转换为相同的数据类型。在自动转换时,对于数值类型,系统将低精度的数据类型转换为高精度的数据类型。
在包括多个查询的UNION语句中,其执行顺序是自左至右,使用括号可以改变这一执行顺序。例如:
查询1 UNION (查询2 UNION 查询3)
INTERSECT,EXCEPT INTERSECT是交集. EXCEPT是差集的意思 select user_id from user intersect select user_id from toy; //就是返回这两个select查询相同的部分.(这里就是有玩具的孩子的user_id) select user_id from user except select user_id from toy; //就是返回第一个select 减去第二个select 结果的部分。(这里就是没有玩具的孩子的user_id)
二、连接查询
通过连接运算符可以实现多个表查询。连接是关系数据库模型的主要特点,也是它区别于其它类型数据库管理系统的一个标志。
在标准SQL中的UNION语句如下:
SELECT column_name(s) FROM table_name1 UNION SELECT column_name(s) FROM table_name2
其中有前提:每个SELECT的内容(表项)必须是相同的结构。详细的说,链接的表的列数必须互相相同,同时相对的列属性也必须相同。而列名可以不同(结构相同)
首先交叉连接:cross join
select u.username, t.toyname from user as u cross join toy as t;
//交叉连接就是把所有第一个表和第二个表的值一一对应
//例如:假如第一个表格有5个值,第二个表有4个值,这个结果就应该有20条记录。
内联接:inner join
相等连接:(equal join)
select u.username, t.toyname from user as u inner join toy as t on(也可以用where) u.user_id = t.user_id; //这个的结果就是各自拥有的玩具了。
不等连接:(non-equal join)
select u.username, t.toyname from user as u inner join toy as t on(也可以用where) u.user_id <> t.user_id order by u.username; //这个的结果是各自没有的玩具
自然连接:(natural join)
这个是有在连接的两个表格中的列名称相等的时候才能用。
select u.username, t.toyname from user as u natural join toy as t; //和上面的相等连接的结果是一样的。
外连接:outer join
左外联接、右外联接比较相似:
left outer join ; right outer join; select u.usrename, t.toyname from user u inner join toy t on (这里不能用where) u.user_id = t.user_id; //这个是查询用户姓名和他所拥有的玩具名字 select u.username, t.toyname from user u left outer join toy t on u.user_id = t.user_id; //这个是以左表(user)为基准,来查询所有用户姓名和他所拥有的玩具名字。 select u.username, t.toyname from user u right outer join toy t on u.user_id = t.user_id; //这个是以右表(toy)为基准,来查询所有玩具的拥有者姓名,对于没有拥有者的为NULL
以上就是Mysql联合,连接查询_MySQL的内容,更多相关内容请关注PHP中文网(www.php.cn)!

This article explores optimizing MySQL memory usage in Docker. It discusses monitoring techniques (Docker stats, Performance Schema, external tools) and configuration strategies. These include Docker memory limits, swapping, and cgroups, alongside

This article addresses MySQL's "unable to open shared library" error. The issue stems from MySQL's inability to locate necessary shared libraries (.so/.dll files). Solutions involve verifying library installation via the system's package m

The article discusses using MySQL's ALTER TABLE statement to modify tables, including adding/dropping columns, renaming tables/columns, and changing column data types.

This article compares installing MySQL on Linux directly versus using Podman containers, with/without phpMyAdmin. It details installation steps for each method, emphasizing Podman's advantages in isolation, portability, and reproducibility, but also

This article provides a comprehensive overview of SQLite, a self-contained, serverless relational database. It details SQLite's advantages (simplicity, portability, ease of use) and disadvantages (concurrency limitations, scalability challenges). C

This guide demonstrates installing and managing multiple MySQL versions on macOS using Homebrew. It emphasizes using Homebrew to isolate installations, preventing conflicts. The article details installation, starting/stopping services, and best pra

Article discusses configuring SSL/TLS encryption for MySQL, including certificate generation and verification. Main issue is using self-signed certificates' security implications.[Character count: 159]

Article discusses popular MySQL GUI tools like MySQL Workbench and phpMyAdmin, comparing their features and suitability for beginners and advanced users.[159 characters]


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)
