按此顺序根据主 ID 更新列
这是测试代码。我希望更新 city_id,以便 city_id 必须与 select 语句中出现的该城市的第一个 id 匹配。例子。在这里,我希望 city_id [最后一列] 为 1,1,3,3
create table student2 (id int not null primary key identity,
city_name varchar(25),
student_name varchar(25),
city_id int null)
insert into student2 values('Boston','Nome',null)
insert into student2 values('Boston','Tiger',null)
insert into student2 values('Miami','Andy',null)
insert into student2 values('Miami','Moran',null)
两个查询是如果这样就可以了。我显然有大量记录。创建临时表并将结果输出到文本文件也可以。在这种情况下,您将打印 id 和 city_id
Here is the test code. I would like the city_id to be updated such that the city_id must must match the first id of that city that appears in select statement. Example. Here I would like the city_id [last column] to be 1,1,3,3
create table student2 (id int not null primary key identity,
city_name varchar(25),
student_name varchar(25),
city_id int null)
insert into student2 values('Boston','Nome',null)
insert into student2 values('Boston','Tiger',null)
insert into student2 values('Miami','Andy',null)
insert into student2 values('Miami','Moran',null)
Two query's are fine if that does the job. I have obviously large number of records. Creating temporary table and outputting the result to text file is fine too. In that case you would print id and city_id
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最好的方法是创建一个名为
cities
的新表,其中包含列city_id
和city_name
。这称为规范化数据库,并降低问题的复杂性。对于您的示例,您将拥有:
The best approach is to create a new table called
cities
, that contains columnscity_id
andcity_name
. This is called normalizing the database, and reduces the complexity of your problem.For your example, you would have:
就像这样:
祝你好运!
Like this:
Good luck!