Compare data SQL statements from two tables in MySQL

Source: Internet
Author: User

There are two tables T_appinfo and g_appinfo, the stored information is basically consistent, all contain package_name this field, now need to find out package_name exists in the T_appinfo table, and does not exist in the G_appinfo table records.

Method One: Use Not EXISTS

The code is as follows Copy Code


SELECT A.name,a.package_name from T_appinfo a
WHERE not EXISTS (SELECT 1 from G_appinfo g where a.package_name=g.p_packagename) LIMIT 10000

Method Two: Use Not in

The code is as follows Copy Code

Select A.name, a.package_name from T_appinfo a WHERE a.package_name not in (SELECT g.p_packagename from G_appinfo g) LIMIT 50000

When two tables have more data, for example, more than 100,000, you need to add index to package_name, improve query efficiency.

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.