Search code examples
asp.netsqljoinsql-server-2008r2-express

SQL Server : get name for each id in a row from another table (ASP.net)


I am developing a site in VS2010 using SQL Server 2008 R2 Express and C#

My tables are like:

Table 1

https://i.sstatic.net/3XeSr.png

I want to write a query that will select all rows from table 1 but instead of showing choice1 or (2..3)'s ID, it will grab their name from table 2

How can I do it?

Thanks in advance


Solution

  • You can get data from this two table by join the same table three time.

           SELECT table2.name AS choice1name, table2_1.name AS choice2name, table2_2.name AS                                                                                                                                                                                                                                                                                                choice3name
           FROM  table1 INNER JOIN
                 table2 ON table1.choice1 = table2.id INNER JOIN
                 table2 AS table2_1 ON table1.choice2 = table2_1.id INNER JOIN
                 table2 AS table2_2 ON table1.choice3 = table2_2.id