| |

VerySource

 Forgot password?
 Register
Search
View: 826|Reply: 7

Two tables of data integration

[Copy link]

1

Threads

3

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-1-31 11:20:01
| Show all posts |Read mode
if object_id ('a') is not null drop table a
go
CREATE TABLE a (
       [teacherid] [varchar] (50)
       [courseinfo] [varchar] (50),
       [coursename] [varchar] (50),
       [point] [float],
      
 )

insert into a (courseinfo, coursename, point, bjjh) values ​​('0001', '0101011002_1_1', 'Magma and Metamorphic Petrology_Theory', 80.05)
insert into a (courseinfo, coursename, point, bjjh) values ​​('0002', '0101011003_1_1', 'Magma_Theory', 90.50)
insert into a (courseinfo, coursename, point, bjjh) values ​​('0003', '0101011006_1_1', 'Metamorphic Petrology_Theory', 90.50)
insert into a (courseinfo, coursename, point, bjjh) values ​​('0004', '0101011007_1_1', 'Geographic Informatics_Theory', 90.50)
go

if object_id ('b') is not null drop table a
go
CREATE TABLE b (
       [teacherid] [varchar] (50)
       [courseinfo] [varchar] (50),
       [coursename] [varchar] (50),
       [point] [float],
      
 )

insert into a (courseinfo, coursename, point, bjjh) values ​​('0001', '0101011002_1_1', 'Magma and Metamorphic Petrology_Theory', 83.05)
insert into a (courseinfo, coursename, point, bjjh) values ​​('0003', '0101011006_1_1', 'Metamorphic Petrology_Theory', 95.50)
insert into a (courseinfo, coursename, point, bjjh) values ​​('0005', '0101011008_1_1', 'Geographic Informatics_Theory', 90.50)
go

The data in the above two tables want to be integrated into one table. In table a, there are teachers and their scores, but in table b, there is no one. Table b automatically adds one to table a, but the teacher's point item is 0 points, and the same b The table has the same treatment as table a.
Reply

Use magic Report

0

Threads

35

Posts

22.00

Credits

Newbie

Rank: 1

Credits
22.00

 China

Post time: 2020-3-28 20:15:01
| Show all posts
do not understand
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-3-30 05:15:01
| Show all posts
insert into a
select * from b where courseinfo not in (select courseinfo from a)
Reply

Use magic Report

0

Threads

40

Posts

29.00

Credits

Newbie

Rank: 1

Credits
29.00

 China

Post time: 2020-3-31 15:45:01
| Show all posts
Should the sql provided by lz be modified to:

CREATE TABLE a (
       [teacherid] [varchar] (50),
       [courseinfo] [varchar] (50),
       [coursename] [varchar] (50),
       [point] [float]
      
 )

insert into a (teacherid, courseinfo, coursename, point) values ​​('0001', '0101011002_1_1', 'Magma and Metamorphic Petrology_Theory', 80.05)
insert into a (teacherid, courseinfo, coursename, point) values ​​('0002', '0101011003_1_1', 'Magma_theory', 90.50)
insert into a (teacherid, courseinfo, coursename, point) values ​​('0003', '0101011006_1_1', 'Metamorphic Petrology_Theory', 90.50)
insert into a (teacherid, courseinfo, coursename, point) values ​​('0004', '0101011007_1_1', 'Geographic Informatics_Theory', 90.50)
go

CREATE TABLE b (
       [teacherid] [varchar] (50),
       [courseinfo] [varchar] (50),
       [coursename] [varchar] (50),
       [point] [float]
      
 )
insert into b (teacherid, courseinfo, coursename, point) values ​​('0001', '0101011002_1_1', 'Magma and Metamorphic Petrology_Theory', 83.05)
insert into b (teacherid, courseinfo, coursename, point) values ​​('0003', '0101011006_1_1', 'Metamorphology_Theory', 95.50)
insert into b (teacherid, courseinfo, coursename, point) values ​​('0005', '0101011008_1_1', 'Geographic Informatics_Theory', 90.50)
go
Reply

Use magic Report

0

Threads

40

Posts

29.00

Credits

Newbie

Rank: 1

Credits
29.00

 China

Post time: 2020-3-31 20:45:01
| Show all posts
insert into a
select teacherid, courseinfo, coursename, point 0
from b
where teacherid not in (select distinct teacherid from a)
Reply

Use magic Report

0

Threads

40

Posts

29.00

Credits

Newbie

Rank: 1

Credits
29.00

 China

Post time: 2020-4-1 09:00:02
| Show all posts
I just wrote it wrong, so
insert into a
select teacherid, courseinfo, coursename, 0
from b
where teacherid not in (select distinct teacherid from a)
Reply

Use magic Report

1

Threads

3

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

 Author| Post time: 2020-7-22 14:00:01
| Show all posts
if object_id('a') is not null drop table a
go
CREATE TABLE a (
       [teacherid] [varchar] (50),
       [courseinfo] [varchar] (50),
       [coursename] [varchar] (50),
       [point] [float],
      
)

insert into a(teacherid,courseinfo,coursename,point) values('0001','0101011002_1_1','magmatic rock and metamorphic petrology_theory', 80.05)
insert into a(teacherid,courseinfo,coursename,point) values('0002','0101011003_1_1','magmatology_theory',90.50)
insert into a(teacherid,courseinfo,coursename,point) values('0003','0101011006_1_1','metamorphic petrology_theory',90.50)
insert into a(teacherid,courseinfo,coursename,point) values('0004','0101011007_1_1','geoinformatics_theory',90.50)
go

if object_id('b') is not null drop table a
go
CREATE TABLE b (
       [teacherid] [varchar] (50),
       [courseinfo] [varchar] (50),
       [coursename] [varchar] (50),
       [point] [float],
      
)

insert into a(teacherid,courseinfo,coursename,point) values('0001','0101011002_1_1','magmatic rock and metamorphic petrology_theory', 83.05)
insert into a(teacherid,courseinfo,coursename,point) values('0003','0101011006_1_1','metamorphic petrology_theory',95.50)
insert into a(teacherid,courseinfo,coursename,point) values('0005','0101011008_1_1','geoinformatics_theory',90.50)
go

The statement to generate the table is like this. It was written in a hurry that day and there was no time to verify it. I'm really sorry!
Reply

Use magic Report

1

Threads

3

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

 Author| Post time: 2020-7-22 15:45:01
| Show all posts
if object_id('a') is not null drop table a
go
CREATE TABLE a (
       [teacherid] [varchar] (50),
       [courseinfo] [varchar] (50),
       [coursename] [varchar] (50),
       [point] [float],
      
)

insert into a(teacherid,courseinfo,coursename,point) values('0001','0101011002_1_1','magmatic rock and metamorphic petrology_theory', 80.05)
insert into a(teacherid,courseinfo,coursename,point) values('0002','0101011003_1_1','magmatology_theory',90.50)
insert into a(teacherid,courseinfo,coursename,point) values('0003','0101011006_1_1','metamorphic petrology_theory',90.50)
insert into a(teacherid,courseinfo,coursename,point) values('0004','0101011007_1_1','geoinformatics_theory',90.50)
go

if object_id('b') is not null drop table b
go
CREATE TABLE b (
       [teacherid] [varchar] (50),
       [courseinfo] [varchar] (50),
       [coursename] [varchar] (50),
       [point] [float],
      
)

insert into b(teacherid,courseinfo,coursename,point) values('0001','0101011002_1_1','magmatic rock and metamorphic petrology_theory', 83.05)
insert into b(teacherid,courseinfo,coursename,point) values('0003','0101011006_1_1','metamorphic petrology_theory',95.50)
insert into b(teacherid,courseinfo,coursename,point) values('0005','0101011008_1_1','geoinformatics_theory',90.50)
go

select * from a
select * from b
----The above section of the generated table
---This answer is interesting, but it has not fully answered my question
insert into a
select teacherid,courseinfo,coursename,0
from b
where teacherid not in(select distinct teacherid from a
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