| |

VerySource

 Forgot password?
 Register
Search
View: 1130|Reply: 10

SQL Server UPDATE eligible field after comparing two table fields

[Copy link]

1

Threads

4

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-1-11 16:40:02
| Show all posts |Read mode
There are 2 tables in the library, table A and table B
The fields in table A are as follows
ID AREA CITYID
1 Beijing Chaoyang District NULL
2. Nanshan District, Shenzhen, Guangdong, China 1 NULL
3. Futian District, Shenzhen City, Guangdong Province NULL
4. Nanshan District, Shenzhen City, Guangdong Province 2 NULL
5. Guangdong Province NULL
Table B fields
ID CITY SZCODE
Beijing 110000
2. Chaoyang District 110105
3. Guangdong Province 440000
4. Shenzhen 440300
5. Nanshan District 440305
Now I want to fill the CITYID in table A with the SZCODE in table B
Insert CITYID after comparing AREA of table A and CITY of table B
The data in table A is about 300,000, and the data in table B is the administrative division of China.
The effect of table A replacement is as follows:
ID AREA CITYID
1 Beijing Chaoyang District NULL (110105)
2. Nanshan District, Shenzhen, Guangdong, China (440305)
3. Futian District, Shenzhen City, Guangdong Province NULL (440303)
4. Nanshan District, Shenzhen City, Guangdong Province 2 NULL (440305)
5. Guangdong Province NULL (440000)
Just learned SQL, please help ...
Reply

Use magic Report

0

Threads

14

Posts

12.00

Credits

Newbie

Rank: 1

Credits
12.00

 China

Post time: 2020-1-15 21:45:01
| Show all posts
update A set a.CITYID = b.SZCODE from A a, B b where a.ID = b.ID

or

update A set a.CITYID = b.SZCODE from A a inner join B b on a.ID = b.ID
Reply

Use magic Report

0

Threads

211

Posts

108.00

Credits

Newbie

Rank: 1

Credits
108.00

 China

Post time: 2020-1-16 06:27:01
| Show all posts
--try

update A set CITYID = (select max (B.SZCODE) as SZCODE from B where charindex (B.CITY, A.AREA)> 0)
Reply

Use magic Report

0

Threads

211

Posts

108.00

Credits

Newbie

Rank: 1

Credits
108.00

 China

Post time: 2020-1-16 09:27:01
| Show all posts
update A set CITYID = B.SZCODE
from B
where charindex (B.CITY, A.AREA)> 0
Reply

Use magic Report

0

Threads

30

Posts

25.00

Credits

Newbie

Rank: 1

Credits
25.00

 China

Post time: 2020-1-16 12:27:01
| Show all posts
update A set a.CITYID = b.SZCODE
from A a
inner join B b on a.ID = b.ID and charindex (B.CITY, A.AREA)> 0
Reply

Use magic Report

0

Threads

30

Posts

25.00

Credits

Newbie

Rank: 1

Credits
25.00

 China

Post time: 2020-1-16 15:18:01
| Show all posts
--correct:
update A set a.CITYID = b.SZCODE
from A a, B b
where charindex (B.CITY, A.AREA)> 0
Reply

Use magic Report

0

Threads

66

Posts

27.00

Credits

Newbie

Rank: 1

Credits
27.00

 China

Post time: 2020-1-16 17:36:01
| Show all posts
select a. *, b.SZCODE, charindex (CITY, AREA) as a into # from a, b where charindex (CITY, AREA)> 0

update a set CITYID = SZCODE from # t where a.AREA = t.AREA and not exists (select * from # where id = t.id and a> t.a)

drop table #

select * from a

--result

1 Beijing Chaoyang District 110105
2 Nanshan District, Shenzhen, Guangdong 1 440305
3 Futian District, Shenzhen, Guangdong 440300
4 Nanshan District, Shenzhen, Guangdong 2 440305
5 Guangdong Province 440000


The landlord requires a minimum level match
Reply

Use magic Report

0

Threads

30

Posts

25.00

Credits

Newbie

Rank: 1

Credits
25.00

 France

Post time: 2020-1-17 11:54:01
| Show all posts
Positive solution upstairs, there will be multiple matches when where charindex (B.CITY, A.AREA)> 0, whichever is greater
Because the maximum value indicates that the area is behind
Reply

Use magic Report

1

Threads

4

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-1-17 12:27:02
| Show all posts
En is the lowest level match and then rises in order
For example: Nanshan District, Shenzhen City, Guangdong Province
If there is no Nanshan District in the B table, then follow the Shenzhen operation.
The AREA field in table A contains many records of Nanshan District
In fact, the information in the IP library is matched with the administrative area, so that the actual project operation can be performed.
(City, district is automatically selected based on IP).
If the information in the IP library (Table A) is directly used, it is not very standardized, so it needs to be matched with the administrative district data table (Table B) and used.
Reply

Use magic Report

1

Threads

4

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-1-17 20:18:01
| Show all posts
Table A == ip Table B == City
Field AREA === F_ip_area CITYID == F_city_id SZCODE == sz_code CITY == city
Is it converted to the following?
Hey, do n’t make a joke, just contact. If the conversion is successful, I take out the database. It is convenient for everyone to use. I see that many people need such a function. (The area is more standardized, which is convenient for practical application.)
select ip. *, City.sz_code, charindex (city, F_ip_area) as ip into # from ip, City where charindex (city, F_ip_area)> 0

update ip set F_city_id = sz_code from # t where ip.F_ip_area = t.F_ip_area and not exists (select * from # where id = t.id and ip> t.ip)

drop table #

select * from ip
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list