5

i have 2 tables

User Code         SubMenuID

usercol           menucol 
-----------       -------------
AB                Sub-01 
Alam              Sub-02 
CSRL

i want to show them like this

usercol           menucol
----------        ------------
AB                Sub-01 
AB                Sub-02 
Alam              Sub-01
Alam              Sub-02 
CSRL              Sub-01 
CSRL              Sub-02

How can i get this using sql query? It would be very helpful :)

Saharsh Shah
  • 28,687
  • 8
  • 48
  • 83
Anupam Roy
  • 1,654
  • 2
  • 18
  • 25

3 Answers3

10

Since the tables are not related by a foreign key relationship, you can not join them - what you want as a result, is the Cartesian product from the two tables. This is achieved by selecting from both tables without any additional join condition (this is also called a cross join):

mysql> SELECT * FROM userCode, SubMenuId;

This query combines all rows from the first table with all rows from the second table.

+---------+---------+
| usercol | menucol |
+---------+---------+
| AB      | Sub-01  |
| AB      | Sub-02  |
| Alam    | Sub-01  |
| Alam    | Sub-02  |
| CSRL    | Sub-01  |
| CSRL    | Sub-02  |
+---------+---------+
Andreas Fester
  • 36,091
  • 7
  • 95
  • 123
0

Try this:

SELECT a.usercol, b.menucol FROM UserCode a JOIN SubMenuID b
Saharsh Shah
  • 28,687
  • 8
  • 48
  • 83
0
select * from usercode,submenuid
order by usercol;
Ajith Sasidharan
  • 1,155
  • 7
  • 7