【问题标题】:How can I select / insert / update data from another table into a new table如何从另一个表中选择/插入/更新数据到新表中
【发布时间】:2021-06-12 11:17:30
【问题描述】:

我有一个这样的类别表 - 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

我有另一个国家表 - 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

我需要一个选择查询来显示信息并输入运费字段。

我使用 Gridview .NET 执行此操作。

感谢您帮助编写查询以进行选择和插入、更新

【问题讨论】:

  • 请详细说明您的需求,TB_Countries 与其他表的关系如何?您要更新和插入哪个表
  • 你说得对,我认为最好有一个国家表,表 TB_numbercategories 外键的 id,你怎么看?
  • 只要说清楚,您需要定义要从查询中更新的表
  • @iAm.Hassan 听起来很棒,它会更容易显示,下次我建议你像程序员/开发人员一样问,不要让我们学习你的功能方面。

标签: sql join select sql-update sql-insert


【解决方案1】:

架构:

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小提琴here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-25
    • 2014-02-17
    • 1970-01-01
    • 1970-01-01
    • 2015-04-04
    • 1970-01-01
    相关资源
    最近更新 更多