MySQL query to set different combinations for values in a table?


Let us first create a table −

mysql> create table DemoTable622 (Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,Value1 int,Value2 int);
Query OK, 0 rows affected (1.08 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable622(Value1,Value2) values(1000,1000);
Query OK, 1 row affected (0.23 sec)
mysql> insert into DemoTable622(Value1,Value2) values(2000,3000);
Query OK, 1 row affected (1.04 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable622;

This will produce the following output −

+----+--------+--------+
| Id | Value1 | Value2 |
+----+--------+--------+
|  1 | 1000   | 1000   |
|  2 | 2000   | 3000   |
+----+--------+--------+
2 rows in set (0.00 sec)

Here is the query to MySQL combinations −

mysql> select tbl1.Id,tbl2.Value1,tbl3.Value2 from DemoTable622 tbl1,DemoTable622 tbl2,DemoTable622 tbl3;

This will produce the following output −

+----+--------+--------+
| Id | Value1 | Value2 |
+----+--------+--------+
|  1 | 1000   |   1000 |
|  2 | 1000   |   1000 |
|  1 | 2000   |   1000 |
|  2 | 2000   |   1000 |
|  1 | 1000   |   3000 |
|  2 | 1000   |   3000 |
|  1 | 2000   |   3000 |
|  2 | 2000   |   3000 |
+----+--------+--------+
8 rows in set (0.01 sec)

Updated on: 23-Aug-2019

317 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements