| |

VerySource

 Forgot password?
 Register
Search
View: 976|Reply: 6

Ask a simple SQL statement, thank you ~

[Copy link]

1

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-2-7 12:00:01
| Show all posts |Read mode
Form A:

Place of origin
Nanjing Shanghai
Shanghai Beijing
 ...
 ...
 ...

b table
1 Shanghai
2 Nanjing
3 Beijing


Now show:

Place of origin
twenty one
1 3
 ...
 ...
 ...
Reply

Use magic Report

0

Threads

126

Posts

73.00

Credits

Newbie

Rank: 1

Credits
73.00

 China

Post time: 2020-3-28 10:45:02
| Show all posts
select B. area ID AS sending place, C. area ID AS receiving place
FROM A table A LEFT JOIN b table B ON A. Issued place = B. Place name
 LEFT JOIN b Table C ON A. Receiving place = C. Place name
Reply

Use magic Report

0

Threads

114

Posts

69.00

Credits

Newbie

Rank: 1

Credits
69.00

 China

Post time: 2020-3-28 12:00:01
| Show all posts
update table1 set issued = b.id from table2 b where issued = b.region
update table1 set receiving place = b.id from table2 b where receiving place = b.area
select * from table1
Reply

Use magic Report

0

Threads

211

Posts

108.00

Credits

Newbie

Rank: 1

Credits
108.00

 Korea, Republic of

Post time: 2020-3-28 16:15:01
| Show all posts
create table A (originating nvarchar (10), receiving nvarchar (10))
insert A select 'Nanjing', 'Shanghai'
union all select 'Shanghai', 'Beijing'

create table B (id int, col nvarchar (10))
insert B select 1, 'Shanghai'
union all select 2, 'Nanjing'
union all select 3, 'Beijing'

select send = tmpA.id, receive = tmpB.id from A
left join B as tmpA on A. issued = tmpA.col
left join B as tmpB on A. receiving place = tmpB.col
 
--result
Place of origin
----------- -----------
twenty one
1 3

(2 row (s) affected)
Reply

Use magic Report

0

Threads

88

Posts

55.00

Credits

Newbie

Rank: 1

Credits
55.00

 China

Post time: 2020-3-28 21:00:01
| Show all posts
select (select id from b table where name = a. sending place) 'sending place', (select id from b table where name = a. receiving place) 'receiving place' from table A
Reply

Use magic Report

0

Threads

49

Posts

35.00

Credits

Newbie

Rank: 1

Credits
35.00

 China

Post time: 2020-3-28 22:00:02
| Show all posts
select t1.id as sending place, t2.id as receiving place
from a inner join b t1 on a. emitted = t1. name
       inner join b t2 on a. receiving place = t2. name
Reply

Use magic Report

0

Threads

11

Posts

9.00

Credits

Newbie

Rank: 1

Credits
9.00

 China

Post time: 2020-3-31 23:45:01
| Show all posts
--Use custom functions
create table a (departure place varchar (10), receiving place varchar (10))
insert into a
select 'Beijing', 'Nanjing'
union all select 'Nanjing', 'Shanghai'
union all select 'Shanghai', 'Beijing'
go
create table b (id int, name varchar (10))
insert into b
select 1, 'Shanghai'
union all select
2, 'Nanjing'
union all select
3, 'Beijing'
go
create function getid (@name varchar (10))
returns int
as
begin
declare @id int
select @ id = id from b where name = @ name
return @id
end
go
select dbo.getid (departure place) as departure place, dbo.getid (receive place) as receive place from a

drop function getid
drop table a
drop table b

--result
Place of departure
3 2
twenty one
1 3
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