由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Database版 - SQL question: update a field
相关主题
SQL combine two tables into one table and add a new column最近写了不少SQL script,请大牛评价下属于什么水平
SQL copy a table into a new table and add a new column紧急求助, 关于SQL Server
新手请教SQL 语法问题- alias 和 join问个SQL问题- partial outer join
请教set和select 的区别error of executing SQL query of string concatenation (转载
Access 里面两个 column不一样的table 能combine 到一起吗?Error of SQL query on IBM netezza SQL database from Aginity (转载)
SQL question...网站遭受Hack, 有哪位知道如何处理?
问一道SQL的题 (转载)PostgreSQL现在好像用得地方多起来了
请求SQL语句Senior Database Developer Position for Hedge Fund
相关话题的讨论汇总
话题: table1话题: sql话题: max话题: update话题: columns
进入Database版参与讨论
1 (共1页)
s**********i
发帖数: 711
1
table1 has bunch of columns, with 'id' as primary key, and
I need to update a column a with max value of table2.b, for
corresponding id.
I tried
replace into table1 (id, a) select id, max(b) from table2 group by id;
this can put the max(b) into table1 a column, but in same time overwrites
all other columns in table1 to default value... which certainly is not
what I want.
anyone know an answer? TIA
m******t
发帖数: 2416
2

Right now I can think of a way to do it in two steps -
select each distinct id, and its max b value into a temp table,
then update table1 with this temp table.
I guess it's possible to do it with one single sql, but I
can't experiment any of my thoughts at this point...

【在 s**********i 的大作中提到】
: table1 has bunch of columns, with 'id' as primary key, and
: I need to update a column a with max value of table2.b, for
: corresponding id.
: I tried
: replace into table1 (id, a) select id, max(b) from table2 group by id;
: this can put the max(b) into table1 a column, but in same time overwrites
: all other columns in table1 to default value... which certainly is not
: what I want.
: anyone know an answer? TIA

s**********i
发帖数: 711
3
thanks. since this likely can't be done in one step,
I've decide just modify my scripts to do it in the
program rather than SQL alone.
f***g
发帖数: 10
4

If using SQL Server, these 2 statements can be combined as one like this
UPDATE table1
SET a = TEMP2.maxB
FROM table1
INNER JOIN (select id, max(b) as maxB from table2 group by id) TEMP2
ON table1.id = TEMP2.id
Not sure about other DBs such as Oracle...
xt
发帖数: 17532
5

If it is on SQL Server, most probably Sybase ASE supports it too.

【在 f***g 的大作中提到】
:
: If using SQL Server, these 2 statements can be combined as one like this
: UPDATE table1
: SET a = TEMP2.maxB
: FROM table1
: INNER JOIN (select id, max(b) as maxB from table2 group by id) TEMP2
: ON table1.id = TEMP2.id
: Not sure about other DBs such as Oracle...

s**********i
发帖数: 711
6

thanks... but it's not SQL server... I'm not going to say it
as aya would teach me again to use postgresql :)

【在 f***g 的大作中提到】
:
: If using SQL Server, these 2 statements can be combined as one like this
: UPDATE table1
: SET a = TEMP2.maxB
: FROM table1
: INNER JOIN (select id, max(b) as maxB from table2 group by id) TEMP2
: ON table1.id = TEMP2.id
: Not sure about other DBs such as Oracle...

t****s
发帖数: 141
7
MySql? haha

【在 s**********i 的大作中提到】
:
: thanks... but it's not SQL server... I'm not going to say it
: as aya would teach me again to use postgresql :)

1 (共1页)
进入Database版参与讨论
相关主题
Senior Database Developer Position for Hedge FundAccess 里面两个 column不一样的table 能combine 到一起吗?
Rookie's question againSQL question...
SQL add some columns into a table from another table (转载问一道SQL的题 (转载)
sql里怎么做循环?请求SQL语句
SQL combine two tables into one table and add a new column最近写了不少SQL script,请大牛评价下属于什么水平
SQL copy a table into a new table and add a new column紧急求助, 关于SQL Server
新手请教SQL 语法问题- alias 和 join问个SQL问题- partial outer join
请教set和select 的区别error of executing SQL query of string concatenation (转载
相关话题的讨论汇总
话题: table1话题: sql话题: max话题: update话题: columns