由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Database版 - SQL copy a table into a new table and add a new column
相关主题
菜鸟问题,急新手请教SQL 语法问题- alias 和 join
sort two same tables SQL but different results请问sql 有条件性的select columns
请教一个sql问题sql 题目求助
Access 里面两个 column不一样的table 能combine 到一起吗?谁能帮我看看这个Insert语句要怎么改一下?
请教一个SQL的问题请教一个query 优化的问题(filter keyword)
如何除去duplicate rows?请教一个SQL Server的面试题
SQL select one value column for each distinct value another (转载)SQL question: update a field
SQL combine two tables into one table and add a new column怎样快速得到两个表的交集
相关话题的讨论汇总
话题: table1话题: table话题: new话题: table2话题: col4
进入Database版参与讨论
1 (共1页)
l******9
发帖数: 579
1
need to copy a table into a new table on SQL server 2008. Also, add a new
column into the new table.
The values of the new column depends on the compare result between the new
table and another table.
Example,
Table1:
col1 col2 col3
abc 346 6546
hth 549 974
Table1_new:
col1 col2 col3 col4
abc 346 6546 1
hth 549 974 0
Table2:
col1
abc
sfsdf
If Table2's col1 appear in Table1 col1, mark col4 as 1 in Table1_new, else
mark as 0.
The code does not work
SELECT *,
(
SELECT 1 as col4
FROM Table2 as a
INNER JOIN Table1 as b
on b.col1 = a.col1
SELECT 0 as col4
FROM Table2 as a
INNER JOIN Table1 as b
on b.col1 <> a.col1 # I do not know how to do this !!!
)
INTO table1_new
FROM table1
Any help would be appreciated.
y*******7
发帖数: 219
2
any PK or unique col?
s**********o
发帖数: 14359
3
其实就是UPDATE一个新COL的问题吧
a**d
发帖数: 4285
4
执行了一下,可行:
select t1.col1, t1.col2, t1.col3,
col4= case when t2.col1 is not null then 1
else 0
end
into table_new
from table1 t1 left join table2 t2 on t1.col1=t2.col1
s**********o
发帖数: 14359
5
OMG,LZ基本的SQL都没学会啊
B*****g
发帖数: 34098
6
YHD

【在 s**********o 的大作中提到】
: OMG,LZ基本的SQL都没学会啊
1 (共1页)
进入Database版参与讨论
相关主题
怎样快速得到两个表的交集请教一个SQL的问题
请问个join的问题如何除去duplicate rows?
请求SQL语句SQL select one value column for each distinct value another (转载)
求助:如何ColumName 作为变量放入querySQL combine two tables into one table and add a new column
菜鸟问题,急新手请教SQL 语法问题- alias 和 join
sort two same tables SQL but different results请问sql 有条件性的select columns
请教一个sql问题sql 题目求助
Access 里面两个 column不一样的table 能combine 到一起吗?谁能帮我看看这个Insert语句要怎么改一下?
相关话题的讨论汇总
话题: table1话题: table话题: new话题: table2话题: col4