要提取具有指定公共值的元组,请使用以下语法-
SELECT DISTINCT aliasName.yourColumnName1,aliasName.yourColumnName2,aliasName1.yourColumnName 1,aliasName1.yourColumnName2 FROM yourTableName aliasName INNER JOIN yourTableName aliasName1 ON aliasName.yourColumnName1 = aliasName1.yourColumnName1 WHERE aliasName.yourColumnName2 = 'value1' AND aliasName1.yourColumnName2 = 'value2';
为了理解上述语法,让我们创建一个表。创建表的查询如下-
mysql> create table extractTuples -> ( -> Id int, -> Name varchar(20), -> Comments text -> );
使用insert命令在表中插入一些记录。查询如下-
mysql> insert into extractTuples values(1,'John','hi'); mysql> insert into extractTuples values(2,'Carol','hello'); mysql> insert into extractTuples values(3,'John','Amazing'); mysql> insert into extractTuples values(1,'Carol','Good');
使用select语句显示表中的所有记录。查询如下-
mysql> select *from extractTuples;
输出结果
+------+-------+----------+ | Id | Name | Comments | +------+-------+----------+ | 1 | John | hi | | 2 | Carol | hello | | 3 | John | Amazing | | 1 | Carol | Good | +------+-------+----------+ 4 rows in set (0.00 sec)
这是提取具有指定公共值的元组的查询-
mysql> SELECT DISTINCT tbl.Id,tbl.Name,tbl1.Id,tbl1.Name -> FROM extractTuples tbl -> INNER JOIN extractTuples tbl1 -> ON tbl.Id = tbl1.Id -> WHERE tbl.Name = 'John' AND tbl1.Name = 'Carol';
输出结果
+------+------+------+-------+ | Id | Name | Id | Name | +------+------+------+-------+ | 1 | John | 1 | Carol | +------+------+------+-------+ 1 row in set (0.00 sec)