Using INNERJOIN in MySQL to implement Intersect Union operations _ MySQL

Source: Internet
Author: User
MySQL uses INNERJOIN to implement Intersect Union operations MySQL uses inner join to implement Intersect Union operations
I. Business Background

We have a table design as follows:

CREATE TABLE `user_defined_value` (  `RESOURCE_ID` varchar(20) DEFAULT NULL,  `COLUMN_NAME` varchar(20) DEFAULT NULL,  `VALUE` varchar(255) DEFAULT NULL,  KEY `ID_IDX` (`RESOURCE_ID`)) ENGINE=MyISAM DEFAULT CHARSET=utf8;

RESOURCE_ID is the unique identifier of a resource.

COLUMN_NAME is an attribute name of a resource.

VALUE is the attribute VALUE of a resource.


II. requirements

Now you need to find the resource ID of attribute A = '1' and attribute B = '2' from the table.


III. Analysis

Here, we need to check the resource IDs that satisfy both attributes. if the relationship is OR, the SQL statement will be very simple.

SELECT DISTINCT RESOURCE_ID FROM USER_DEFINED_VALUE WHERE (COLUMN_NAME = 'A' AND VALUE in ('1'))  OR (COLUMN_NAME = 'B' AND VALUE in ('2'))

When the relationship is the same, here we cannot simply replace the OR keyword with AND. we need to first check the resources that meet each attribute AND then obtain the union set.

On Baidu, I found that SQL Server and Oracle both support Intersect. we can directly take the query results of two identical structures to the intersection, and MySQL does not have the Intersect keyword. how can we achieve this?


4. use inner join xxx USING xxx

SELECT DISTINCT RESOURCE_ID FROM USER_DEFINED_VALUE INNER JOIN (SELECT DISTINCT RESOURCE_ID FROM USER_DEFINED_VALUE WHERE  (COLUMN_NAME = 'A' AND VALUE in ('1')) ) t0 USING (RESOURCE_ID) INNER JOIN (SELECT DISTINCT RESOURCE_ID FROM USER_DEFINED_VALUE WHERE  (COLUMN_NAME = 'B' AND VALUE in ('2')) ) t1 USING (RESOURCE_ID)

In the first statement, all resource IDs in the table are identified.

In the second sentence, locate the resource ID of attribute A = '1' and obtain the intersection with the query result of the first sentence.

In the third sentence, locate the resource ID of attribute B = '2' and obtain the intersection with the preceding query result.

In this way, you can add other attributes.

Copy the translation results from Google or

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.