English 中文(简体)
Update a table with records from another table
原标题:

I am trying to update my table 1 with some field value from table 2 based on a condition. But am unable to get the proper query. Here s the condition:

I have a table 1 with date field which should be updated from table 2 date field. The condition is that the id of table 1 should be equal to id of table 2 (Table 2 has id of table 1 as FK). Another condition should be there is a varchar field in table 2 which should have specific value, say "Test". Wherever the value of field in table 2 is "Test" I want to update the date of with that record in table 1 date field. But there is another catch. It may be possible that more than 1 records for same id in table 2 can have value as "Test"

I was trying the query as:

UPDATE A
SET A.Date = Max(B.[Date])
FROM Table1 A
 INNER JOIN Table2 B ON A.ID = B.FK_ID
WHERE (B.Changed LIKE  Test )
 AND A.Date IS NULL

But this is not working as sql does not allow Max in update when there is no group by. Please help. A bit urgent.

最佳回答

You need to create another inner join where the ID s of Table1 are coupled with the maximum dates of Table2 like so:

UPDATE    Table1
SET       Date = BDate.MaxDate
FROM      Table1 A
          INNER JOIN Table2 B ON A.ID = b.FK_ID
          INNER JOIN (
              SELECT    A.ID, [MaxDate] = MAX(B.Date)
              FROM      Table1 A
                        INNER JOIN Table2 B ON A.ID = b.FK_ID
              GROUP BY  A.ID
          ) BDate ON BDate.ID = A.ID
WHERE     B.Changed LIKE  Test 
          A.Date IS NULL
问题回答

You could always use subqueries:

UPDATE Table1 a SET
    [Date] = (SELECT MAX([Date]) FROM Table2 b WHERE a.ID = b.FK_ID AND b.Changed LIKE  Test )
WHERE [Date] IS NULL




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

难以执行 REGEXP_SUBSTR

I m 查询Oracle 10g。 我有两张表格(样本数据见下文)。 i m 试图提取一些领域

SQL Query Shortcuts

What are some cool SQL shorthands that you know of? For example, something I learned today is you can specify to group by an index: SELECT col1, col2 FROM table GROUP BY 2 This will group by col2

PHP array callback functions for cleaning output

I have an array of output from a database. I am wondering what the cleanest way to filter the values is example array Array ( [0] => Array ( [title] => title 1 ...

OracleParameter and DBNull.Value

we have a table in an Oracle Database which contains a column with the type Char(3 Byte). Now we use a parameterized sql to select some rows with a DBNull.Value and it doesn t work: OracleCommand ...

Running numbers in SQL

I have a SQL-statement like this: SELECT name FROM users WHERE deleted = 0; How can i create a result set with a running number in the first row? So the result would look like this: 1 Name_1 2 ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签