简体   繁体   English

在列中查找不同的值并在sql中添加另一列

[英]Finding distinct values in column and adding another column in sql

If I have a table with columns a and b as shown 如果我有一个带有列a和b的表,如图所示

a b
1 1
1 10
1 20
2 11
2 21
3 31

I want to insert into this table the distinct values in column a along with an arbitrary constant, let's say 0. So I want the output to be: 我想在表中插入列a中的不同值以及一个任意常量,假设为0。所以我希望输出为:

a b
1 1
1 10
1 20
2 11
2 21
3 31
1 0
2 0
3 0

How can I use INSERT INTO with DISTINCT to accomplish this? 如何通过DISTINCT使用INSERT INTO来完成此任务? I'm not sure how to incorporate the arbitrary constant 我不确定如何合并任意常量

只需按照您所说的,插入选择区:

insert into tablename select distinct a, 0 from tablename

Pretty straightforward: 很简单:

insert into your_Table (a, b)
select distinct a, 0
from your_Table

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM