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

My tables are like:

Table 1

http://i.stack.imgur.com/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

有帮助吗?

解决方案

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

其他提示

Looks like you need an inner joins. I had the same situation few weeks ago and i was referred to the link I am giving you. Look here to find out which one will work for you. here or this one Inner joins

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top