0

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

My tables are like:

Table 1

https://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

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Sakir Khan
  • 15
  • 1
  • 5

2 Answers2

0

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
Raghubar
  • 2,768
  • 1
  • 21
  • 31
  • I am not joining table1's id with table2's id but `table1.choice1 to table2.id` then same way...`table1.choice2 to table2.id` and.. `table1.choice3 to table2.id` so its a kind of many-to-one relation – Sakir Khan Nov 02 '13 at 06:56
  • What u want. Do u want to show name in place of choice1, choice2 and choice3 ? – Raghubar Nov 02 '13 at 07:01
0

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

Community
  • 1
  • 1
FeliceM
  • 4,163
  • 9
  • 48
  • 75