MySQL学习足迹记录14--表别名和自联结_MySQL
bitsCN.com
MySQL学习足迹记录14--表别名和自联结
本查询所用到的表:
下面的表num代表公共汽车路线,如1路车,2路车,stop带表停车站点,如A站,B站
表:
route;+------+------+| num | stop |+------+------+| 1 | A || 1 | B || 1 | C || 2 | B || 2 | C || 2 | D |+------+------+
准备知识
1.使用表别名
*表别名只在查询执行中使用
*表别名不返回到客户机
Example: mysql> SELECT * from route AS r1 -> WHERE r1.num = 1;+------+------+| num | stop |+------+------+| 1 | A || 1 | B || 1 | C |+------+------+3 rows in set (0.00 sec)
解析:
相信理解表别名应该不费力,类似与列别名,表别名只是给表取另外一个名字而已,代表的还是相同的表
2.自联结
*自联结通常作为外部语句来代替从相同表中检索数据时使用的子查询语句
*使用表别名能在单条语句中多次使用相同的表
下面给出一个简单的例子帮助理解自联结的原理
Example:
下面的语句查询的结果是共用同一车站的所有公交线
mysql> SELECT DISTINCT r2.num,r2.stop -> FROM route AS r1,route AS r2 -> WHERE r1.stop = r2.stop -> ORDER BY r2.stop;+------+------+| num | stop |+------+------+| 1 | A || 1 | B || 2 | B || 1 | C || 2 | C || 2 | D |+------+------+6 rows in set (0.00 sec)
解析:
第一句
mysql> SELECT DISTINCT r2.num,r2.stop -> FROM route AS r1,route AS r2;
为了详解,这里先去除DISTINCT关键字,并只截取下面的子句
mysql> select * From route AS r1,route r2;+------+------+------+------+| num | stop | num | stop |+------+------+------+------+| 1 | A | 1 | A || 1 | B | 1 | A || 1 | C | 1 | A || 2 | B | 1 | A || 2 | C | 1 | A || 2 | D | 1 | A || 1 | A | 1 | B || 1 | B | 1 | B || 1 | C | 1 | B || 2 | B | 1 | B || 2 | C | 1 | B || 2 | D | 1 | B || 1 | A | 1 | C || 1 | B | 1 | C || 1 | C | 1 | C || 2 | B | 1 | C || 2 | C | 1 | C || 2 | D | 1 | C || 1 | A | 2 | B || 1 | B | 2 | B || 1 | C | 2 | B || 2 | B | 2 | B || 2 | C | 2 | B || 2 | D | 2 | B || 1 | A | 2 | C || 1 | B | 2 | C || 1 | C | 2 | C || 2 | B | 2 | C || 2 | C | 2 | C || 2 | D | 2 | C || 1 | A | 2 | D || 1 | B | 2 | D || 1 | C | 2 | D || 2 | B | 2 | D || 2 | C | 2 | D || 2 | D | 2 | D |+------+------+------+------+36 rows in set (0.00 sec)
从上面的查询结果中可以看出,共有36条记录(刚好是两张表的笛卡尔积,关于笛卡尔积,请点击MySQL学习足迹记录13--联结表),
可一推测,所谓的自联结就是把同一张表,看成独立的,不同的两张表r1,r2
我们的目的是查询共用同一车站的所有公交线,所以从上表的结果集中再添加筛选条件(令车站相等):
r1.stop = r2.stopmysql> SELECT * FROM route AS r1,route AS r2 -> WHERE r1.stop = r2.stop; +------+------+------+------+| num | stop | num | stop |+------+------+------+------+| 1 | A | 1 | A || 1 | B | 1 | B || 2 | B | 1 | B || 1 | C | 1 | C || 2 | C | 1 | C || 1 | B | 2 | B || 2 | B | 2 | B || 1 | C | 2 | C || 2 | C | 2 | C || 2 | D | 2 | D |+------+------+------+------+10 rows in set (0.00 sec)
这已经很接近所需的结果了,但我们只需num,stop两列,再添加
SELECT r2.num,r2.stop (SELECT r1.num,r1.stop也OK) mysql> SELECT r2.num,r2.stop -> FROM route AS r1,route r2 -> WHERE r1.stop = r2.stop;+------+------+| num | stop |+------+------+| 1 | A || 1 | B || 1 | B || 1 | C || 1 | C || 2 | B || 2 | B || 2 | C || 2 | C || 2 | D |+------+------+10 rows in set (0.00 sec)
最后去除相同的记录,再按车站排序
mysql> SELECT DISTINCT r2.num,r2.stop -> FROM route AS r1,route AS r2 -> WHERE r1.stop = r2.stop -> ORDER BY r2.stop;+------+------+| num | stop |+------+------+| 1 | A || 1 | B || 2 | B || 1 | C || 2 | C || 2 | D |+------+------+6 rows in set (0.00 sec)
从结果集中可以看出1路车和2路 车共用B,C车站
bitsCN.com
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



Pinduoduo software provides a lot of good products, you can buy them anytime and anywhere, and the quality of each product is strictly controlled, every product is genuine, and there are many preferential shopping discounts, allowing everyone to shop online Simply can not stop. Enter your mobile phone number to log in online, add multiple delivery addresses and contact information online, and check the latest logistics trends at any time. Product sections of different categories are open, search and swipe up and down to purchase and place orders, and experience convenience without leaving home. With the online shopping service, you can also view all purchase records, including the goods you have purchased, and receive dozens of shopping red envelopes and coupons for free. Now the editor has provided Pinduoduo users with a detailed online way to view purchased product records. method. 1. Open your phone and click on the Pinduoduo icon.

How to View Command History in Linux In Linux, we use the history command to view the list of all previously executed commands. It has a very simple syntax: history Some options for pairing with the history command include: Option description -c clears the command history for the current session -w writes the command history to a file -r reloads the command history from the history file -n Limit the number of output of recent commands Simply run the history command to see a list of all previously executed commands in a Linux terminal: In addition to viewing command history, you can also manage command history and perform modifications to previously executed commands , reverse search command history or even delete history completely

Call recording in iPhone is often underestimated and is one of the most critical features of iPhone. With its simplicity, this feature is of vital importance and can provide important insights about the calls made or received on the device. Whether for work purposes or legal proceedings, the ability to access call records can prove invaluable. In simple terms, call history refers to the entries created on your iPhone whenever you make or receive a call. These logs contain key information, including the contact's name (or number if not saved as a contact), timestamp, duration, and call status (dialed, missed, or not answered). They are a concise record of your communication history. Call history includes call history strips stored on your iPhone

iPhone lets you add medications to the Health app to track and manage the medications, vitamins and supplements you take every day. You can then log medications you've taken or skipped when you receive a notification on your device. After you log your medications, you can see how often you took or skipped them to help you track your health. In this post, we will guide you to view the log history of selected medications in the Health app on iPhone. A short guide on how to view your medication log history in the Health App: Go to the Health App>Browse>Medications>Medications>Select a Medication>Options&a

Just like human walking, self-driving cars also need to have the ability to think independently and make judgments and decisions about the traffic environment in order to complete the travel process. With the improvement of advanced assisted driving system technology, the safety of drivers driving cars continues to improve, and the degree of driver participation in driving decision-making is gradually reduced. Autonomous driving is getting closer and closer to us. Self-driving cars, also known as driverless cars, are essentially highly intelligent robots that can complete travel behaviors with only driver assistance or without driver operation at all. Autonomous driving is mainly realized through the perception layer, decision-making layer and execution layer. As an automated vehicle, autonomous vehicles can use additional radar (millimetre-wave radar, lidar), vehicle cameras, and global navigation satellite systems (G

C# Development Suggestions: Logging and Monitoring System Summary: In the software development process, logging and monitoring systems are crucial tools. This article will introduce the role and implementation suggestions of logging and monitoring systems in C# development. Introduction: Logging and monitoring are essential tools in large-scale software development projects. They can help us understand the running status of the program in real time and quickly discover and solve problems. This article will discuss how to use logging and monitoring systems in C# development to improve software quality and development efficiency. The role of logging system

01 What is a skateboard chassis? The so-called skateboard chassis integrates the battery, electric transmission system, suspension, brakes and other components on the chassis in advance to achieve separation of the body and chassis and decoupling the design. Based on this type of platform, car companies can significantly reduce early R&D and testing costs, while quickly responding to market demand to create different models. Especially in the era of driverless driving, the layout of the car is no longer centered on driving, but will focus on space attributes. The skateboard-type chassis can provide more possibilities for the development of the upper cabin. As shown in the picture above, of course when we look at the skateboard chassis, we should not be framed by the first impression of "Oh, it is a non-load-bearing body" when we come up. There were no electric cars back then, so there were no battery packs worth hundreds of kilograms, no steering-by-wire system that could eliminate the steering column, and no brake-by-wire system.

How to log and monitor Java development projects 1. Background introduction With the rapid development of the Internet, more and more companies have begun to develop Java and build various types of applications. In the development process, logging and monitoring are an important link that cannot be ignored. Through logging and monitoring, developers can discover and solve problems in time to ensure the stability and security of applications. 2. The importance of logging 1. Problem tracking: When an application error occurs, logging can help us quickly locate the problem.
