How to find intersection in mysql

青灯夜游
Release: 2022-04-12 15:08:16
Original
10551 people have browsed it

In mysql, you can use the "SELECT" statement and the "INNER JOIN" keyword to query the intersection and find the intersection data. The syntax is "SELECT field name FROM data table 1 INNER JOIN data table 2 USING (field name) ;".

How to find intersection in mysql

The operating environment of this tutorial: windows7 system, mysql8 version, Dell G3 computer.

Create two tables

CREATE TABLE `object_a` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `oname` varchar(50) DEFAULT NULL, `odesc` varchar(50) DEFAULT NULL, `create_time` datetime DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1
Copy after login

Add data

##

CREATE TABLE `object_b` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `oname` varchar(50) DEFAULT NULL, `odesc` varchar(50) DEFAULT NULL, `create_time` datetime DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1
Copy after login
Add data

Query intersection

SELECT a.oname,a.odesc FROM object_a a INNER JOIN object_b b ON a.oname=b.oname AND a.odesc=b.odesc
Copy after login
is equivalent to

SELECT a.oname,a.odesc FROM object_a a INNER JOIN object_b b USING(oname,odesc)
Copy after login
The result is as follows


PS: You can try this writing method for other databases

SELECT oname,odesc FROM object_a INTERSECT SELECT oname,odesc FROM object_b
Copy after login
[Related recommendations:

mysql video tutorial]

The above is the detailed content of How to find intersection in mysql. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!