- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Get all the records with two different values in another column with MySQL
For this, you can use GROUP BY HAVING clause.
Let us first create a table −
mysql> create table DemoTable751 ( StudentName varchar(100), SubjectName varchar(100) ); Query OK, 0 rows affected (0.66 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable751 values('John','MySQL'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable751 values('John','MongoDB'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable751 values('Sam','MySQL'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable751 values('Carol','Java'); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable751 values('David','MySQL'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable751 values('Carol','MongoDB'); Query OK, 1 row affected (0.17 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable751;
This will produce the following output -
+-------------+-------------+ | StudentName | SubjectName | +-------------+-------------+ | John | MySQL | | John | MongoDB | | Sam | MySQL | | Carol | Java | | David | MySQL | | Carol | MongoDB | +-------------+-------------+ 6 rows in set (0.00 sec)
Following is the query to get all the records with two different values in MySQL −
mysql> select StudentName from DemoTable751 group by StudentName having count(SubjectName) > 1;
This will produce the following output -
+-------------+ | StudentName | +-------------+ | John | | Carol | +-------------+ 2 rows in set (0.00 sec)
- Related Articles
- Add records from corresponding duplicate values in another column with MySQL
- Concatenate two values from the same column with different conditions in MySQL
- Get the sum of last 3 digits from all the values in a column with MySQL
- Extract tuples with specified common values in another column in MySQL?
- Get the time difference between values in different columns with MySQL
- Find specific records from a column with comma separated values in MySQL
- MySQL query to get string from one column and find its position in another column with comma separated values?
- Group by one column and display corresponding records from another column with a separator in MySQL
- MySQL query to copy records from one table to another with different columns
- Concatenate rows on the basis of boolean values in another column with MySQL
- Get Nth weekday of the month from a column with DATE records in MySQL
- Get the maximum count of distinct values in a separate column with MySQL
- Can I get the count of repeated values in a column with MySQL?
- Find records with a specific last digit in column with MySQL
- Shuffling column values with MySQL?

Advertisements