Asim Das
Asim Das

Reputation: 93

join of two sql tables

I have two tables and I want to join them to get the desired output. Say the 1st table (seat1) is

enter image description here

and the 2nd table (collegestudents) is

enter image description here

The desired output is

enter image description here

I have tried the below code. But it fails to give the desired result.

    $rde2=mysqli_query($con, "select * from seat1 s
                              left JOIN collegestudents c ON c.Roll = s.Roll
                              ");            




     Any help please.

Upvotes: 0

Views: 39

Answers (1)

GMB
GMB

Reputation: 222482

You want a left join. Your query looks fine, but you would need not to use select *, and instead explictly list the columns that you want to select, using table prefixes. Otherwise, since you have a Roll column in both tables, a name clashes will happen, that your application apparently does not handle well.

select 
    s.Roll,
    c.Name,
    s.Subject
from seat1 s
left join collegestudents c on c.Roll = s.Roll

Upvotes: 3

Related Questions