Home Backend Development PHP Tutorial Related explanations about left join on and where condition placement

Related explanations about left join on and where condition placement

May 09, 2018 am 09:13 AM
join left where

left The placement of join on and where conditions is very important in PHP. This article will explain the relevant knowledge in detail.

With and in left joinQuery

SELECT p.pname,p.pcode,s.saletime from product as p left join sales_detail as s on (s.pcode=p.pcode) and s.saletime in ('2012-07-23','2012-07-05');

The result found:

------- ------- ------------

| pname | pcode | saletime |

-- ----- ------- ------------

| A | AC | 2012-07-23 |

| A | AC | 2012-07-05 |

| A | AC | 2012-07-05 |

##| B | DE |

NULL |

| C |

##SELECT p.pname,p.pcode,s.saletime from product as p left join sales_detail as s on (s.pcode=p.pcode) where s.saletime in ('2012-07-23', '2012-07-05');

Query results

------- ------- ----------- -

| pname | pcode | saletime |

------- ------- ------------

| A | AC | 2012-07-23 |

| A | AC | 2012-07-05 |

##| A | AC | 2012-07-05 |

------- ------- ------------

Conclusion: Conditions in on Association, when the data in a table does not meet the conditions, a null value will be displayed. where will output the two tables that completely meet the conditional data


The conditions in the left join: the benchmark data of the left table will be used, and all the data that appears in the left table will appear, and then To join the right table, you need to find out as long as there is a correlation. If the corresponding field has no value or does not meet the conditions, it is set to NULL.

SELECT p.pname,p.pcode,s.saletime from product as p left join sales_detail as s on (s.pcode=p.pcode) ;If you just left join, the displayed content is as follows


------- ------- ------------

| pname | pcode | saletime |

------- ------- ------------

| A | AC | 2012-07-23 |

| A | AC | 2012-07-05 |

| A | AC | 2012-07-05 |

| B | DE | 2012-07-16 | There is a value in it

| C | XXX | NULL | There is no value in it

------- ------- --------- ---

If it has a value but does not meet the conditions, it will be set to NULL. If there is no value, it must be NULL

If it is a where condition, it must be satisfied.

Application scenario: For example, if there is a main table, then you can consider left join to display data based on the main table.

Summary:

1. For left join, no matter what condition is followed by on, all the data in the left table will be found. Therefore, if you want to filter, you need to put the condition after where

2. For inner join, the data in the table that satisfies the condition after on Only then can it be detected, which can play a filtering role. You can also put the condition after where.

The difference between on condition and where condition in SQL

When the database returns records by connecting two or more tables, it will generate an intermediate temporary table, and then return this temporary table to the user.

When using left jion, the difference between on and where conditions is as follows:

1. The on condition is a condition used when generating a temporary table. It does not matter whether the condition in on is true. will return the records in the table on the left.

2. The where condition is the condition for filtering the temporary table after the temporary table is generated. At this time, there is no meaning of left join (the records of the left table must be returned). If the condition is not true, all will be filtered out.

This article explains the knowledge related to the placement of left join and where conditions. For more learning materials, please pay attention to the php Chinese website.

Related recommendations:

Related knowledge about php mysql fuzzy query function

How to copy and move files through php


About jQuery effects-hiding and showing related knowledge

The above is the detailed content of Related explanations about left join on and where condition placement. For more information, please follow other related articles on the PHP Chinese website!

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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.

A Practical Guide to the Where Method in Laravel Collections A Practical Guide to the Where Method in Laravel Collections Mar 10, 2024 pm 04:36 PM

Practical Guide to Where Method in Laravel Collections During the development of the Laravel framework, collections are a very useful data structure that provide rich methods to manipulate data. Among them, the Where method is a commonly used filtering method that can filter elements in a collection based on specified conditions. This article will introduce the use of the Where method in Laravel collections and demonstrate its usage through specific code examples. 1. Basic usage of Where method

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

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 the Where method in Laravel collections How to use the Where method in Laravel collections Mar 10, 2024 pm 10:21 PM

How to use the Where method in Laravel collection Laravel is a popular PHP framework that provides a wealth of functions and tools to facilitate developers to quickly build applications. Among them, Collection is a very practical and powerful data structure in Laravel. Developers can use collections to perform various operations on data, such as filtering, mapping, sorting, etc. In collections, the Where method is a commonly used method for filtering the collection based on specified conditions.

From beginner to proficient: Master the skills of using is and where selectors From beginner to proficient: Master the skills of using is and where selectors Sep 08, 2023 am 09:15 AM

From beginner to proficient: Master the skills of using is and where selectors Introduction: In the process of data processing and analysis, the selector is a very important tool. Through selectors, we can extract the required data from the data set according to specific conditions. This article will introduce the usage skills of is and where selectors to help readers quickly master the powerful functions of these two selectors. 1. Use of the is selector The is selector is a basic selector that allows us to select the data set based on given conditions.

See all articles