Home Database Mysql Tutorial 有意思的left join语句

有意思的left join语句

Jun 07, 2016 pm 02:53 PM
join left statement

有意思的left join语句 问题的初衷是,在一个SQL语句中,有left join, 我加条件在where后面发生死锁,于是想是否可以把条件加入到left join的on后面? www.2cto.com 也就是下面两个语句是否效果一样: 语句1: select t1.*,t2.* from t1 left join t2 on t1


有意思的left join语句

 

问题的初衷是,在一个SQL语句中,有left join, 我加条件在where后面发生死锁,于是想是否可以把条件加入到left join的on后面?  www.2cto.com  

也就是下面两个语句是否效果一样:

语句1: select t1.*,t2.* from t1 left join t2 on t1.id=t2.id and t1.feild=1

 

语句2: select t1.*,t2.* from t1 left join t2 on t1.id=t2.id where t1.feild=1

 

数据环境:

create table t1(id int, feild int);

insert into t1 values(1 , 1);

insert into t1 values(1 , 2);

insert into t1 values(1 , 3);

insert into t1 values(1 , 4);

insert into t1 values(2 , 1);

insert into t1 values(2 , 2);

create table t2(id int, feild int);

insert into t2 values(1 , 1);

insert into t2 values(1 , 2);

insert into t2 values(1 , 5);

insert into t2 values(1 , 6);

insert into t2 values(2 , 1);

insert into t2 values(2 , 3);

 

经过验证发现: 语句1可以这样来理解,把left join 的on后面的所有条件看成一个整体,这里就是t1.id=t2.id and t1.feild=1看成一个整体,坐连接的时候,串t2表,看是否有满足条件的记录,如果没有就串null值。

语句2的理解简单了,现在left join,在最后结果集中做where过滤

 

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use JOIN in MySql How to use JOIN in MySql Jun 04, 2023 am 08:02 AM

The meaning of JOIN is just like the English word "join". It joins two tables and can be roughly divided into inner join, outer join, right join, left join and natural join. First create two tables, the following is used as an example CREATETABLEt_blog(idINTPRIMARYKEYAUTO_INCREMENT,titleVARCHAR(50),typeIdINT);SELECT*FROMt_blog;+----+------+--------+| id|title|typeId|+----+-------+--------+|1|aaa|1||2|bbb|2||3|ccc|3|

Which key is left on the keyboard? Which key is left on the keyboard? Mar 13, 2023 pm 02:27 PM

The left keyboard is the left direction key, and the right is the right direction key. Generally, the keyboard is replaced by symbols or arrows. Some keyboards are labeled in English. The keyboard is an instruction and data input device used to operate the equipment, and also refers to the system arrangement. A set of function keys that operate a machine or piece of equipment.

What is the usage principle of MySQL Join? What is the usage principle of MySQL Join? May 26, 2023 am 10:07 AM

Join type leftjoin uses the left table as the driving table and the left table as the basis of the result set. The data from the right table is connected to the result set. rightjoin uses the right table as the driving table and the right table as the basis of the result set to connect the left table. The data is added to the result set innerjoin. The result set takes the intersection of the two tables fulljoin. The result set takes the union of the two tables. MySQL does not have a fulljoin. The difference between union and unionall is that union will deduplicate the crossjoin Cartesian product. If the where condition is not used, the result set will be the product and of the two associated table rows. The difference is that when crossjoin creates the result set, it will be passed according to the on condition.

What are mysql's join query and multiple query methods? What are mysql's join query and multiple query methods? Jun 02, 2023 pm 04:29 PM

Compared with join query and multiple queries, which one is more efficient, MySQL multi-table related query or multiple single-table query? When the amount of data is not large enough, there is no problem using join, but it is usually done on the service layer. First: the computing resources of a stand-alone database are very expensive, and the database needs to serve both writing and reading at the same time, which requires CPU consumption. In order to make the database The throughput becomes higher, and the business does not care about the delay gap of hundreds of microseconds to milliseconds. The business will put more calculations into the service layer. After all, computing resources can be easily expanded horizontally, and databases are difficult, so most The business will put pure computing operations into the service layer, and use the database as a kv system with transaction capabilities. This is a heavy business.

How to use JOIN in MySQL How to use JOIN in MySQL Jun 03, 2023 am 09:30 AM

Introduction A's unique + AB's public B's unique + AB's public AB's public A's unique B's unique A's unique + B's unique + AB's public A's unique + B's unique Practice creating table department tables DROPTABLEIFEXISTS`dept`;CREATETABLE`dept`(`dept_id`int(11)NOTNULLAUTO_INCREMENT,`dept_name`varchar(30)DEFAULTNULL,`dept_number`int(11)DEFAULTNULL,PRIMARYKEY(`dept_id`))ENGINE =InnoDBAUT

Use MySQL's JOIN function to join tables Use MySQL's JOIN function to join tables Jul 26, 2023 am 08:37 AM

Use MySQL's JOIN function to join tables. In MySQL, JOIN is a very common operation that allows us to join two or more tables based on the associated fields between them. This makes it easy to query and obtain relevant data from multiple tables, improving query efficiency and flexibility. This article will use code examples to demonstrate how to use MySQL's JOIN function to join tables. First create two sample tables: students and scores. The students table contains students

multi-catch statement in PHP8.0 multi-catch statement in PHP8.0 May 14, 2023 pm 01:51 PM

With the development of web applications, PHP language has been widely used in web development. In the PHP8.0 version, a new language feature was introduced - the multi-catch statement. What is a multi-catch statement? In previous PHP versions, developers needed to write multiple catch statements to handle multiple exception types. For example, the following code block shows the handling of two different exceptions: try{//Somecodethatmay

Interpretation of CSS positioning properties: position and top/left/right/bottom Interpretation of CSS positioning properties: position and top/left/right/bottom Oct 21, 2023 am 09:58 AM

Interpretation of CSS positioning properties: position and top/left/right/bottom In front-end development, CSS positioning properties are very important. With the positioning attribute, we can control the position of the element on the page. The most commonly used positioning attribute is position, whose values ​​can be static, relative, absolute, and fixed. In addition to these basic positioning attributes, we can also use top, left, r

See all articles