
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
Extract tuples with specified common values in another column in MySQL?
To extract tuples with specified common values, use the following syntax −
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';
To understand the above syntax, let us create a table. The query to create a table is as follows −
mysql> create table extractTuples -> ( -> Id int, -> Name varchar(20), -> Comments text -> ); Query OK, 0 rows affected (0.77 sec)
Insert some records in the table using insert command. The query is as follows −
mysql> insert into extractTuples values(1,'John','hi'); Query OK, 1 row affected (0.20 sec) mysql> insert into extractTuples values(2,'Carol','hello'); Query OK, 1 row affected (0.17 sec) mysql> insert into extractTuples values(3,'John','Amazing'); Query OK, 1 row affected (0.13 sec) mysql> insert into extractTuples values(1,'Carol','Good'); Query OK, 1 row affected (0.14 sec)
Display all records from the table using a select statement. The query is as follows −
mysql> select *from extractTuples;
Output
+------+-------+----------+ | Id | Name | Comments | +------+-------+----------+ | 1 | John | hi | | 2 | Carol | hello | | 3 | John | Amazing | | 1 | Carol | Good | +------+-------+----------+ 4 rows in set (0.00 sec)
Here is the query to extract tuples with specified common values −
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';
Output
+------+------+------+-------+ | Id | Name | Id | Name | +------+------+------+-------+ | 1 | John | 1 | Carol | +------+------+------+-------+ 1 row in set (0.00 sec)
- Related Articles
- Add records from corresponding duplicate values in another column with MySQL
- Get all the records with two different values in another column with MySQL
- Extract the middle part of column values in MySQL surrounded with hyphens and display in a new column?
- Python – Extract tuples with elements in Range
- Concatenate rows on the basis of boolean values in another column with MySQL
- Add a temporary column in MySQL where the values depend on another column?
- Only display specified values inside the IN clause with MySQL?
- MySQL query to get string from one column and find its position in another column with comma separated values?
- Shuffling column values with MySQL?
- Copy column values from one table into another matching IDs in MySQL
- How to repeat column values in R matrix by values in another column?
- MySQL query to group by column and display the sum of similar values in another column
- How to repeat column values in R data frame by values in another column?
- Using MySQL IN() for some column values with underscore
- Pad the values of the column with zeros in MySQL

Advertisements