Mysql left join uses the on and where filter differences, mysqljoin

Source: Internet
Author: User

Mysql left join uses the on and where filter differences, mysqljoin

For example, we now have two tables: Product table (products) and sales_detail (sales record table ). The two tables are used to describe the differences between the left join condition on and where conditions in MySQL.

1. Data Preparation

Create a products table and insert data

Drop table if exists products; create table 'products' ('pid 'INT (3) not null auto_increment, 'pname' VARCHAR (20) not null, 'pcode' VARCHAR (20) not null, primary key ('pid ') ENGINE = MyISAM AUTO_INCREMENT = 1 default charset = utf8; insert into 'products' ('pid', 'pname', 'pcode ') VALUES (1, 'item 1', 'ac90'), (2, 'item 2', 'de78'), (3, 'item 3', 'xxxx ');

Create a sales_detail table and insert data

drop table if exists sales_detail;CREATE TABLE `sales_detail` (    `aid` INT (3) NOT NULL auto_increment,    `pcode` VARCHAR (20) NOT NULL,    `saletime` date NOT NULL,    PRIMARY KEY (`aid`)) ENGINE = MyISAM DEFAULT CHARSET = utf8 AUTO_INCREMENT = 1;INSERT INTO `sales_detail` (`aid`, `pcode`, `saletime`)VALUES    (1, 'AC90', '2008-09-22'),    (2, 'DE78', '2008-09-22'),    (3, 'AC90', '2008-09-23'),    (4, 'AC90', '2008-09-24');

 

The data in the database is as follows:

Products table
Pid Pname Pcode
1 Item 1 AC90
2 Product 2 DE78
3 Item 3 XXXX
Sales_detail table
Aid Pcode Saletime
1 AC90 2008-09-22
2 DE78 2008-09-22
3 AC90 2008-09-23
4 AC90 2008-09-24
2. Test

Now, there is a scenario where the product is ranked by the sales volume in a certain period of time. For example, I want to count and rank the sales volume in the two days of the 23-24. (Note: DE78 is not sold in the past two days, but it must be displayed, but the quantity is 0)

Query using the where condition:

SELECT    p.pname,    p.pcode,    s.saletime,    count(s.aid) AS totalFROM    products AS pLEFT JOIN sales_detail AS s ON (s.pcode = p.pcode)WHERE    s.saletime IN ('2008-09-23', '2008-09-24')GROUP BY    p.pcodeORDER BY    total DESC,    p.pid ASC

Result:

Pname Pcode Saletime Total
Item 1 AC90 2008-09-23 2

 

 

The query process can be divided into two parts. First, an intermediate table is generated using the on condition (three data entries in total), and then the intermediate table is filtered using the where condition to obtain the final result.

Use the on condition query:

SELECT    p.pname,    p.pcode,    s.saletime,    count(s.aid) AS totalFROM    products AS pLEFT JOIN sales_detail AS s ON (    (s.pcode = p.pcode)    AND s.saletime IN ('2008-09-23', '2008-09-24'))GROUP BY    p.pcodeORDER BY    total DESC,    p.pid ASC

Result:

Pname Pcode Saletime Total
Item 1 AC90 2009-09-23 2
Product 2 DE78 NULL 0
Item 3 XXXX NULL 0

 

 

 

 

The on condition is used to obtain the result,Records in the left table are returned no matter whether the conditions on the on clause are true or not. If rows that do not meet the connection conditions are also displayed in the query results, the join conditions must be placed on the on clause.. The preceding query is equivalent:

SELECT    p.pname,    p.pcode,    s.saletime,    count(s.aid) AS totalFROM    products AS pLEFT JOIN  (select * from sales_detail s where s.saletime IN ('2008-09-23', '2008-09-24')) as s ON     (s.pcode = p.pcode)GROUP BY    p.pcodeORDER BY    total DESC,    p.pid ASC

 

3. Conclusion

When the Database connects two or more tables to return records, it will generate a temporary table in the middle and then return this temporary table to the user.

When left jion is used, the on and where conditions are different as follows:

1. The on condition is used to generate a temporary table. It returns records in the left table no matter whether the on condition is true or not.

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.