Here are two tables. I want to UPDATE (NOT to SELECT) the first table based on the info from both tables.
The first table named somecities
:
Name - State - Country - Info1 - Info2
Orlando - FL - US - 123 -AAA
Hrodna - HV - BY - 890 -BBB
The second table named allcities
:
Name - State - Country - Info1
Orlando - FL - US - 123
Orlando - KY - US - 456
Orlando - WV - US - 789
Orlando - SW - SA - 333
Hrodna - HV - BY - 890
Minsk - MV - BY - 199
Any pair of (Name, State, Country) from somecities
table is in the allcities
table, if it matters. So, the allcities
table is bigger.
I want to modify somecities
this way:
1)take the first row, take Orlando
. It is met 4 times in allcities
. So, make 4 strings of Orlando instead of one (add 3 rows to somecities).
2) Take the next city (the second and the last one) - Hrodna
. Hrodna
is met only one time in allcities so we do not add any rows (add zero rows) to somecities
.
3) Do it for each row in somecities
.
The result of the query is the updated somecities
:
Name - State - Country - Info1 - Info2
Orlando - FL - US - 123 - AAA
Orlando - KY - US - 456 - AAA
Orlando - WV - US - 789 - AAA
Orlando - SW - SA - 333 - AAA
Hrodna - HV - BY - 890 - BBB
P.S. As you can see, the values in the Info2 are taken from Info2 for the same city.
Can it be done in one query? Two queries as an answer will be accepted as well.
Did I explain it clear?
Thank you.