if the preview doesnt come out right you can check the text file i post
Hi guys I really need help basically im trying to do a inner join update from table2 to table1. How to do the sql statement for this.
Example
table1
ID Fruit Color ETag Value
1 apple red TAG_TEST 4
2 orange orange TAG_TEST 3
3 cat white TAG_TEST 2
1 apple red TAG_TEST 1
1 apple red TAG_TEST 2
table2
Value Etag
0 BAD
1 Good
2 pretty
3 ok
4 beautiful
table2 has the etag description of the value.
so 0 means bad, 1 means good, 2 means pretty etc.......
Im trying to pull the actual description from table2 and update the etag column where table1.Color -'red'
So basically im searching for the color red and the value red.
Loading

Sam HobbsRank #107 · 20.5K reputationPosted Mar 10, 2010, 9:46 AM EST
SET ETag = Table2.Etag
FROM Table1 INNER JOIN
Table2 ON Table1.Value = Table2.Value
WHERE (Table1.Color = 'red')
David SmithRank #999 · 2K reputationPosted Mar 8, 2010, 1:56 PM EST
ID Fruit Color ETag Value
1 apple red beautiful 4
2 orange orange TAG_TEST 3
3 cat white TAG_TEST 2
1 apple red Good 1
1 apple red pretty 2
Amit ChoudharyRank #145 · 15.8K reputationPosted Mar 8, 2010, 7:00 PM EST
first do one fetch the value for color red from table 1 using following query:
Now you have to update the table 2 on the basis of values coming from the first query:
so the final sql statement will be look like this:
update table2 set Etag='pretty' where Value in (select Value from table1 where color='red')Hope it solve your problem.
Please mark as answer if it helps you.
Sam HobbsRank #107 · 20.5K reputationPosted Mar 8, 2010, 7:41 PM EST
David SmithRank #999 · 2K reputationPosted Mar 8, 2010, 10:08 PM EST
David SmithRank #999 · 2K reputationPosted Mar 9, 2010, 2:48 AM EST
SELECT Table1.ID, Table1.Fruit, Table1.Color, Table2.Etag, Table1.Value INTO Table3
FROM Table2 RIGHT JOIN Table1 ON Table2.Value = Table1.Value;
Lee Sheene6 reputationPosted Mar 9, 2010, 6:23 AM EST
SELECT Table1.ID, Table1.Fruit, Table1.Color, Table2.Etag, Table1.Value
FROM Table2
LEFT OUTER JOIN Table1 ON Table2.Value = Table1.Value
WHERE Table1.Color = 'red';
Sam HobbsRank #107 · 20.5K reputationPosted Mar 9, 2010, 12:17 PM EST
and using the SQL I posted above, the results I get is:
So did I use the wrong input data? Is the output I show above not correct?
Sam HobbsRank #107 · 20.5K reputationPosted Mar 9, 2010, 12:22 PM EST
David SmithRank #999 · 2K reputationPosted Mar 9, 2010, 1:46 PM EST
for table2 there is no ID coulumn
there is just a value of a string representation and Etag column of a string representation
there is no table 3, the result you have for table 3 is is suppose to be for table 1.
table2 is a defintion table basically.
now basically you search table1 Color field for red and look at the value for the row of red
then go to the defnition table 2 and find out what the value you mean
example
if you look at ID 1 which is the first row, there is a red and the value is 4.
so now you go to table2 to find out what 4 mean. In this case 4 means beautiful.
which comes from table 2, so you take the "beautiful" and update or replace in table 1 under the etag column
you only search where there is a red. do it for the whole table.
so the end result of table 1 should be
table1
row 2 and 3 will not be updated because there is no red.
Sam HobbsRank #107 · 20.5K reputationPosted Mar 9, 2010, 3:58 PM EST
WHERE (((Table1.Color)="red"));
David SmithRank #999 · 2K reputationPosted Mar 10, 2010, 12:24 AM EST
David SmithRank #999 · 2K reputationPosted Mar 10, 2010, 12:51 AM EST
Im getting a error saying incorrect syntax near 'RIGHT'
so how can i get it to work in c# sql for the table designer command text
David SmithRank #999 · 2K reputationPosted Mar 11, 2010, 6:17 AM EST
Sam HobbsRank #107 · 20.5K reputationPosted Mar 11, 2010, 6:25 PM EST