深入SELECT语句的查询功能(二)(1)
上接《深入SELECT语句的查询功能(一)》 检索语句与多个表的连接 SELECT语句不仅可以从单个表中检索数据,也可以通过连接多个表来检索数据。这里将介绍全连接和左连接的作用。 我们创建两个表作为例子。 mysql CREATE TABLE first- (- id TINYINT,- first_n
上接《深入SELECT语句的查询功能(一)》
检索语句与多个表的连接
SELECT语句不仅可以从单个表中检索数据,也可以通过连接多个表来检索数据。这里将介绍全连接和左连接的作用。
我们创建两个表作为例子。
mysql> CREATE TABLE first -> ( -> id TINYINT, -> first_name CHAR(10) -> ); Copy after login |
录入如下数据:
+------+-----------+
| id | first_name|
+------+-----------+
| 1 | Tom |
| 2 | Marry |
| 3 | Jarry |
+------+-----------+
mysql> CREATE TABLE last -> ( -> id TINYINT, -> last_name CHAR(10) -> ); Copy after login |
录入数据
+------+-----------+
| id | last_name |
+------+-----------+
| 2 | Stone |
| 3 | White |
| 4 | Donald |
+------+-----------+
全连接
全连接:在检索时指定多个表,将每个表用都好分隔,这样每个表的数据行都和其他表的每行交叉产生所有可能的组合,这样就是一个全连接。所有可能的组和数即每个表的行数之和。
那么观察下面的检索的结果:
mysql> SELECT * FROM first,last;
+------+------------+------+-----------+
| id | first_name | id | last_name |
+------+------------+------+-----------+
| 1 | Tom | 2 | Stone |
| 2 | Marry | 2 | Stone |
| 3 | Jarry | 2 | Stone |
| 1 | Tom | 3 | White |
| 2 | Marry | 3 | White |
| 3 | Jarry | 3 | White |
| 1 | Tom | 4 | Donald |
| 2 | Marry | 4 | Donald |
| 3 | Jarry | 4 | Donald |
+------+------------+------+-----------+
你可以看到输出的结果集中共有3×3=9 行,这就是全连接的结果。
你也可以这样使用SQL语句:
mysql> SELCT first.*,last.* FROM first,last;
输出结果与上面的例子相同,并无二致。记录集的输出的排序是以FROM子句后的表的顺序进行,即先排列位置靠前的表,即使你改变记录集中列的显示顺序,例如下面的例子:
mysql> SELECT last.*,first.* FROM first,last;
+------+-----------+------+------------+
| id | last_name | id | first_name |
+------+-----------+------+------------+
| 2 | Stone | 1 | Tom |
| 2 | Stone | 2 | Marry |
| 2 | Stone | 3 | Jarry |
| 3 | White | 1 | Tom |
| 3 | White | 2 | Marry |
| 3 | White | 3 | Jarry |
| 4 | Donald | 1 | Tom |
| 4 | Donald | 2 | Marry |
| 4 | Donald | 3 | Jarry |
+------+-----------+------+------------+
上面的例子是两个非常小的表的例子,如果是几个非常大的表的全连接,例如,两个行数分别为1000的表,这样的连接可以产生非常大的结果集合1000×1000=100万行。而实际上你并不需要这么多行的结果,通常你需要使用一个WHERE从句来限制返回的记录集的行数:
mysql> SELECT * FROM first,last WHERE first.id= last.id;
+------+------------+------+-----------+
| id | first_name | id | last_name |
+------+------------+------+-----------+
| 2 | Marry | 2 | Stone |
| 3 | Jarry | 3 | White |
+------+------------+------+-----------+

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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

Hot Topics



With the rapid development of the Internet, IP addresses have become an indispensable part of network communications. IP address information is very important in network security monitoring, traffic management, and targeted e-commerce advertising. Therefore, in order to facilitate users to query IP address/domain name information, many websites provide IP address query functions. This article will introduce how to use PHP to implement the IP address query function for readers' reference. 1. What is an IP address? IP address (InternetProtocolAddress) is the network protocol

Asynchronous processing method of SelectChannelsGo concurrent programming using golang Introduction: Concurrent programming is an important area in modern software development, which can effectively improve the performance and responsiveness of applications. In the Go language, concurrent programming can be implemented simply and efficiently using Channels and Select statements. This article will introduce how to use golang for asynchronous processing methods of SelectChannelsGo concurrent programming, and provide specific

How to hide the select element in jquery: 1. hide() method, introduce the jQuery library into the HTML page, you can use different selectors to hide the select element, the ID selector replaces the selectId with the ID of the select element you actually use; 2. css() method, use the ID selector to select the select element that needs to be hidden, use the css() method to set the display attribute to none, and replace selectId with the ID of the select element.

jQuery is a popular JavaScript library that can be used to simplify DOM manipulation, event handling, animation effects, etc. In web development, we often encounter situations where we need to change event binding on select elements. This article will introduce how to use jQuery to bind select element change events, and provide specific code examples. First, we need to create a dropdown menu with options using labels:

Because select allows developers to wait for multiple file buffers at the same time, it can reduce IO waiting time and improve the IO efficiency of the process. The select() function is an IO multiplexing function that allows the program to monitor multiple file descriptors and wait for one or more of the monitored file descriptors to become "ready"; the so-called "ready" state is Refers to: the file descriptor is no longer blocked and can be used for certain types of IO operations, including readable, writable, and exceptions. select is a computer function located in the header file #include. This function is used to monitor file descriptor changes—reading, writing, or exceptions. 1. Introduction to the select function. The select function is an IO multiplexing function.

A closer look at HTTP status code 100: What does it mean? The HTTP protocol is one of the most commonly used protocols in modern Internet applications. It defines the standard specifications required for communication between browsers and web servers. During the process of HTTP request and response, the server will return various types of status codes to the browser to reflect the processing of the request. Among them, HTTP status code 100 is a special status code used to indicate "continue". HTTP status codes consist of three digits, each status code has a specific meaning

1. Keywords in SQL statements are not case-sensitive. SELECT is equivalent to SELECT, and FROM is equivalent to from. 2. To select all columns from the users table, you can use the symbol * to replace the column name. Syntax--this is a comment--query out [all] data from the [table] specified by FEOM. * means [all columns] SELECT*FROM--query out the specified data from the specified [table] from FROM Data of column name (field) SELECT column name FROM table name instance--Note: Use English commas to separate multiple columns selectusername, passwordfrom

Tutorial: Java development steps for implementing the geofence alarm data query function of Amap. Introduction: Amap is a powerful geographic information service platform that provides a wealth of map data and services, including geofence functions. Geofencing is a function that restricts according to the scope of the geographical coordinate system, and can realize monitoring and alarming in regions, regions, etc. In this tutorial, we will introduce how to use Java to develop the geofence alarm data query function of Amap and provide corresponding code examples. Step 1: Apply for Amap to open
