Tuesday, March 27, 2012

Get data condition related information from a single table

If you want to display the data with condition based from a single table.

For example: table name: players

snonamecoach_id
1Sachin1
2Dravid5
3Kohli1
4Ganguly2
5Sehwag4
6Gambir3

Query:
SELECT player.sno, player.name, coach.name AS coachname
FROM players AS coach
JOIN players AS player ON coach.sno = player.coach_id

Output:
snonamecoach_id
1SachinSachin
2DravidSehwag
3KohliSachin
4GangulyDravid
5SehwagGanguly
6GambirKohli

No comments:

Post a Comment