有这样的一个问题mysql查询使用mysql中left(right)join筛选条件在on与where查询出的数据是否有差异。 可能只看着两个关键字看不出任何的问题。那我们使用实际的例子来说到底有没有差异。 例如存在两张表结构 表结构1 Sql代码 drop table ifEXISTSA; CREATE T
有这样的一个问题mysql查询使用mysql中left(right)join筛选条件在on与where查询出的数据是否有差异。
可能只看着两个关键字看不出任何的问题。那我们使用实际的例子来说到底有没有差异。
例如存在两张表结构
表结构1
Sql代码
- drop table if EXISTS A;
- CREATE TABLE A (
- ID int(1) NOT NULL,
- PRIMARY KEY (ID)
- ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
表结构2
Sql代码
- drop table if EXISTS B;
- CREATE TABLE B (
- ID int(1) NOT NULL,
- PRIMARY KEY (ID)
- ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
表一插入数据
Sql代码
- insert into A values ( 1 );
- insert into A values ( 2 );
- insert into A values ( 3 );
- insert into A values ( 4 );
- insert into A values ( 5 );
- insert into A values ( 6 );
表二插入数据
Sql代码
- insert into B values ( 1 );
- insert into B values ( 2 );
- insert into B values ( 3 );
完成后A,B表数据如下:
语句一
Sql代码
- select A.ID as AID, B.ID as BID from A left join B on A.ID = B.ID where B.ID
语句二
Java代码
- select A.ID as AID, B.ID as BID from A left join B on A.ID = B.ID and B.ID3
以上两个语句的查询结果是否一致。
反正一切我是没有注意到这两个查询存在任何差异的【以前也没这么写过sql】。
我们看看实际结果
语句一的查询结果
语句二的查询结果为:
发现两个查询存在差异。
为什么会存在差异,这和on与where查询顺序有关。
我们知道标准查询关键字执行顺序为 from->where->group by->having->order by[ 记得不是很清楚呢]
left join 是在from范围类所以 先on条件筛选表,然后两表再做left join。
而对于where来说在left join结果再次筛选。
第一sql语句查询过程如下等价于:
1:先是left join
Sql代码
- select A.ID as AID, B.ID as BID from A left join B on A.ID = B.ID
查询结果如下
2:再查询结果中将B.ID即BID
也就是我们上面看到的结果。
第二sql语句查询过程如下等价于:
1:先按照on条件刷选表等价于先筛选B表:
2:再已上查询结果与A表做left join,这也是为什么我们看到第二个查询的sql会保留A表的原因。
ON与where的使用一定要注意场所:
(1):ON后面的筛选条件主要是针对的是关联表【而对于主表刷选条件不适用】。
例如
Sql代码
- select A.ID as AID, B.ID as BID from A left join B on A.ID = B.ID and A.ID = 3
这个的查询结果为
挺诧异的吧和我们期望的结果不一样,并为筛选出AID=3的数据。
但是我们也发现 AID 与 中AID 1 于2对应的值为NULL,关联表只取了满足A表筛刷选条件的值。
即主表条件在on后面时附表只取满足主表帅选条件的值、而主表还是取整表。
(2):对于主表的筛选条件应放在where后面,不应该放在ON后面
(3):对于关联表我们要区分对待。如果是要条件查询后才连接应该把查询件
放置于ON后。
如果是想再连接完毕后才筛选就应把条件放置于where后面
(4): 对于关联表我们其实可以先做子查询再做join
所以第二个sql等价于
Sql代码
- select A.ID as AID, B1.ID as BID
- from A left join ( select B.ID from B where B.ID on A.ID = B1.ID
以上全在mysql5.1上测试过

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

MySQL is suitable for small and large enterprises. 1) Small businesses can use MySQL for basic data management, such as storing customer information. 2) Large enterprises can use MySQL to process massive data and complex business logic to optimize query performance and transaction processing.

InnoDB effectively prevents phantom reading through Next-KeyLocking mechanism. 1) Next-KeyLocking combines row lock and gap lock to lock records and their gaps to prevent new records from being inserted. 2) In practical applications, by optimizing query and adjusting isolation levels, lock competition can be reduced and concurrency performance can be improved.

MySQL is not a programming language, but its query language SQL has the characteristics of a programming language: 1. SQL supports conditional judgment, loops and variable operations; 2. Through stored procedures, triggers and functions, users can perform complex logical operations in the database.

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

MySQL is an open source relational database management system suitable for data storage, management, query and security. 1. It supports a variety of operating systems and is widely used in Web applications and other fields. 2. Through the client-server architecture and different storage engines, MySQL processes data efficiently. 3. Basic usage includes creating databases and tables, inserting, querying and updating data. 4. Advanced usage involves complex queries and stored procedures. 5. Common errors can be debugged through the EXPLAIN statement. 6. Performance optimization includes the rational use of indexes and optimized query statements.

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

InnoDB's lock mechanisms include shared locks, exclusive locks, intention locks, record locks, gap locks and next key locks. 1. Shared lock allows transactions to read data without preventing other transactions from reading. 2. Exclusive lock prevents other transactions from reading and modifying data. 3. Intention lock optimizes lock efficiency. 4. Record lock lock index record. 5. Gap lock locks index recording gap. 6. The next key lock is a combination of record lock and gap lock to ensure data consistency.


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

SublimeText3 Chinese version
Chinese version, very easy to use

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Dreamweaver Mac version
Visual web development tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.