我如何 select / 将另一个 table 的数据插入/更新到新的 table
How can I select / insert / update data from another table into a new table
我有一个 table 用于这样的类别 - nid
是一个标识列:
TB_Categories
nid
name
1
Brochure
2
stamp
可以为每个类别订购特定数量,例如:
id
是身份
TB_numbercategories
id
nid
number
1
1
10
2
1
50
3
1
100
4
2
100
5
2
500
6
2
1000
我有另一个 table 国家 - cid
是一个身份列:
TB_Countries
cid
name
1
Germany
2
Netherland
3
Slovakia
到目前为止我已经有了内容,一切都很好。
我遇到的问题是,每个国家/地区的每个类别的每一期的运费都不同。
例如:德国和斯洛伐克的小册子顺序有 100 个不同。
或任何其他国家/地区的任何其他号码。
我需要一个为我显示数据的查询,如下所示:
nid
country
number
transport_cost
1
Germany
10
1
Germany
50
1
Germany
100
1
Netherland
10
1
Netherland
50
1
Netherland
100
1
Slovakia
10
1
Slovakia
50
1
Slovakia
100
我需要一个 select 查询来显示信息,只需输入运费字段即可。
我用 Gridview .NET 做这个操作。
感谢您帮助编写查询 select 并插入、更新
架构:
create table TB_Categories(nid int, name varchar(20));
insert into TB_Categories values(1,'Brochure');
insert into TB_Categories values(2,'stamp');
create table TB_numbercategories (id int, nid int, number int);
insert into TB_numbercategories values(1, 1, 10);
insert into TB_numbercategories values(2, 1, 50);
insert into TB_numbercategories values(3, 1, 100);
insert into TB_numbercategories values(4, 2, 100);
insert into TB_numbercategories values(5, 2, 500);
insert into TB_numbercategories values(6, 2, 1000);
create table TB_Countries (cid int, name varchar(20));
insert into TB_Countries values(1, 'Germany');
insert into TB_Countries values(2, 'Netherland');
insert into TB_Countries values(3, 'Slovakia');
查询:
select tc.nid,tbc.name country ,number,'' Transport_cost from TB_Categories tc inner join TB_numbercategories tnc on tc.nid=tnc.nid
cross join TB_Countries tbc
where tc.name='Brochure'
order by tbc.name,number
GO
输出:
nid
country
number
Transport_cost
1
Germany
10
1
Germany
50
1
Germany
100
1
Netherland
10
1
Netherland
50
1
Netherland
100
1
Slovakia
10
1
Slovakia
50
1
Slovakia
100
db<>fiddle here
我有一个 table 用于这样的类别 - nid
是一个标识列:
TB_Categories
nid | name |
---|---|
1 | Brochure |
2 | stamp |
可以为每个类别订购特定数量,例如:
id
是身份
TB_numbercategories
id | nid | number |
---|---|---|
1 | 1 | 10 |
2 | 1 | 50 |
3 | 1 | 100 |
4 | 2 | 100 |
5 | 2 | 500 |
6 | 2 | 1000 |
我有另一个 table 国家 - cid
是一个身份列:
TB_Countries
cid | name |
---|---|
1 | Germany |
2 | Netherland |
3 | Slovakia |
到目前为止我已经有了内容,一切都很好。
我遇到的问题是,每个国家/地区的每个类别的每一期的运费都不同。
例如:德国和斯洛伐克的小册子顺序有 100 个不同。 或任何其他国家/地区的任何其他号码。
我需要一个为我显示数据的查询,如下所示:
nid | country | number | transport_cost |
---|---|---|---|
1 | Germany | 10 | |
1 | Germany | 50 | |
1 | Germany | 100 | |
1 | Netherland | 10 | |
1 | Netherland | 50 | |
1 | Netherland | 100 | |
1 | Slovakia | 10 | |
1 | Slovakia | 50 | |
1 | Slovakia | 100 |
我需要一个 select 查询来显示信息,只需输入运费字段即可。
我用 Gridview .NET 做这个操作。
感谢您帮助编写查询 select 并插入、更新
架构:
create table TB_Categories(nid int, name varchar(20)); insert into TB_Categories values(1,'Brochure'); insert into TB_Categories values(2,'stamp'); create table TB_numbercategories (id int, nid int, number int); insert into TB_numbercategories values(1, 1, 10); insert into TB_numbercategories values(2, 1, 50); insert into TB_numbercategories values(3, 1, 100); insert into TB_numbercategories values(4, 2, 100); insert into TB_numbercategories values(5, 2, 500); insert into TB_numbercategories values(6, 2, 1000); create table TB_Countries (cid int, name varchar(20)); insert into TB_Countries values(1, 'Germany'); insert into TB_Countries values(2, 'Netherland'); insert into TB_Countries values(3, 'Slovakia');
查询:
select tc.nid,tbc.name country ,number,'' Transport_cost from TB_Categories tc inner join TB_numbercategories tnc on tc.nid=tnc.nid cross join TB_Countries tbc where tc.name='Brochure' order by tbc.name,number GO
输出:
nid country number Transport_cost 1 Germany 10 1 Germany 50 1 Germany 100 1 Netherland 10 1 Netherland 50 1 Netherland 100 1 Slovakia 10 1 Slovakia 50 1 Slovakia 100
db<>fiddle here