MySQL calculates different records for two tables

Source: Internet
Author: User

The problem is: There are two tables, one is to record the records of yesterday and the other is to record the records of today.

(1) Newly Added records: There are records today, not yesterday;

(2) reduced records: there are no records today, and there are records yesterday;

To solve this type of problem, you need to use the SQL connection query.

For the sake of simplicity, use table user1 and table user2 to store the records of yesterday and today

CREATE   TABLE   test4.user1 (    id   int   NOT   NULL,    name   varchar(20)   NOT   NULL,    PRIMARY   KEY   (id));

Then update the table to obtain the following results:

The records of yesterday exist in the user1 table:

The records of yesterday exist in the user2 table:

New record:

select test4.user2.id,test4.user2.namefrom test4.user1 right outer join test4.user2on test4.user1.id = test4.user2.idwhere test4.user1.id is null;

Running result:



Reduced records:

select test4.user1.id, test4.user1.namefrom test4.user1 left outer join test4.user2on test4.user1.id = test4.user2.idwhere test4.user2.id is null;



Additional knowledge points:

Inner join, full outer join, left join, right jion
Combination of inner join tables
Full outer is connected to the same combination of two tables. Table A has data that table B does not have (it is displayed as null), and table B has
Table A does not display (null)
Table A left join table B left join, which is based on table A. All data of Table A is combined by table B. Null is not found.
Table A right join table B right join, based on table B, all data of Table B, some combinations of Table. Null is not found.

Http://lhx1026.iteye.com/blog/512776

Http://database.51cto.com/art/201011/234342.htm

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.