【问题标题】:SQL update query to get values by joining 3 tablesSQL 更新查询通过加入 3 个表来获取值
【发布时间】:2018-02-07 22:58:17
【问题描述】:

请帮我写下面的更新查询。我正在使用 SQL Server 2014。

需求:根据项目ID更新表C,从最新记录中获取对应项目ID对应的'Answer_weightage'值。表 A 'Latest'='Yes' 表示它是最新记录。基于 'Link_Id' 表 A 和 B 被链接。表 A 和 C 基于 'Pr​​oject_Id' 链接

表 A:

<table>
<tr>
<th>
Request Type | 
</th>
<th>Project ID  | </th>
<th>Latest |</th>
<th>	Link_ID</th>
</tr>
<tr>
-----------------------------------------------
</tr>
<tr>
<td>

a	
</td>
<td>1000</td>	<td>No</td>	<td>1</td>
</tr>
<tr>
<td>b</td>	<td>1005</td>	<td>Yes </td>      	<td>2</td>
<tr>
<td>123</td>	<td>1000</td><td>Yes   </td>    	<td>4</td></tr>
<tr>
<td>c	</td><td>1005</td>	<td>No</td>	<td>3</td></tr>
</table>

表 B

<table>
<tr>
<th>
Question</th>	<th>Description</th>	<th>Answer_weightage</th>	<th>Link_ID
</th>

</tr>

<tr><td>
6	</td><td>Question6	</td><td>0</td>	<td>2</td>
</tr>
<tr>
<td>2</td>	<td>Question2</td>	<td>5</td>	<td>4</td></tr>
<tr>
<td>3</td>	<td>Question3	</td><td>5</td>	<td>4</td></tr>
<tr>
<td>4</td>	<td>Question4</td><td>	5</td>	<td>4</td></tr>
<tr>
<td>5	</td><td>Question5	</td><td>2</td>	<td>4</td></tr>
<tr>
<td>6</td>	<td>Question6	</td><td>2	</td><td>4</td></tr>
<tr>
<td>7</td>	<td>Question7</td>	<td>9	</td><td>4</td></tr>
<tr>
<td>1</td>	<td>Question1	</td><td>5</td>	<td>1</td></tr>
<tr>
<td>2</td>	<td>Question2	</td><td>9</td>	<td>1</td></tr>
<tr>
<td>3	</td><td>Question3	</td><td>5</td><td>	1</td></tr>
<tr>
<td>4</td>	<td>Question4</td>	<td>2	</td><td>1</td></tr>
<tr>
<td>5</td>	<td>Question5</td>	<td>5	</td><td>1</td></tr>
<tr>
<td>6</td>	<td>Question6</td>	<td>5	</td><td>1</td></tr>
<tr>
<td>7</td>	<td>Question7</td>	<td>2</td>	<td>1</td></tr>
<tr>
<td>1</td>	<td>Question1</td>	<td>2	</td><td>2</td></tr>
<tr>
<td>2</td>	<td>Question2</td>	<td>0	</td><td>2</td></tr>
<tr>
<td>3	</td><td>Question3</td>	<td>9	</td><td>2</td></tr>
<tr>
<td>4</td>	<td>Question4	</td><td>9</td><td>	2</td></tr>
<tr>
<td>5	</td><td>Question5	</td><td>9</td><td>	2</td></tr>
<tr>
<td>7	</td><td>Question7</td>	<td>5	</td><td>2</td></tr>
<tr>
<td>1</td>	<td>Question1</td><td>	2</td>	<td>4</td></tr>

</table>

表 C:

<table>
<tr>
<th>Project ID</th>
<th>Question1</th>
<th>Question2</th>
<th>Question3</th>
<th>Question4</th>
<th>Question5</th>
<th>Question6</th>
<th>Question7</th>
</tr>
<tr>
<td>1000</td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td>1005</td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</table>

期望的结果(在表 C 中):

    <table>
    <tr>
    <th>Project ID</th>
    <th>Question1</th>
    <th>Question2</th>
    <th>Question3</th>
    <th>Question4</th>
    <th>Question5</th>
    <th>Question6</th>
    <th>Question7</th>
    </tr>
    <tr>
    <td>1000</td>
    <td>2</td>
    <td>5</td>
    <td>5</td>
    <td>5</td>
    <td>2</td>
    <td>2</td>
    <td>9</td>
    </tr>
    <tr>
    <td>1005</td>
    <td>2</td>
    <td>0</td>
    <td>9</td>
    <td>9</td>
    <td>0</td>
    <td>9</td>
    <td>5</td>
    </tr>
    </table>

【问题讨论】:

  • 你有测试过的UPDATE吗?
  • @RadimBača 我对编写 sql 查询非常陌生。这是我现在写的:更新集合 a.[Question]=b.[Answer_weightage] where b.[Description]= "Question1" where b.[link_id]=(select c.[link_id] from table c where c.[project_id]=a.[project_id] and a.[latest]='Yes'
  • 只需将格式化表格剪切并粘贴到代码块中(缩进 4 个空格,例如单击答案编辑框工具栏中的“{}”),不要使用代码 sn-p。请在您的答案中澄清,而不是在 cmets 中。你试过什么?请“展示研究成果”以避免投票。请阅读minimal reproducible example并采取行动。

标签: sql sql-server tsql join sql-update


【解决方案1】:

您要更新表 C 的 question1,将值设置为等于从 B 中选择的 Answer_weightage,其中 b.Description = 'Question1' 和 linkId = (Link_Id from A where latest='YES' and has the same projectId as C)。

Update C set C.Question1 =
 (select Answer_weightage
    from B
    where Description='Question1' and Link_ID =
         (select Link_ID
            from A
            where A.ProjectID = C.ProjectID and Latest='Yes'))

【讨论】:

  • 对于代码块(整行)缩进 4 个空格(点击“{}”),不要使用反引号。
猜你喜欢
  • 2011-05-23
  • 2017-10-27
  • 1970-01-01
  • 2019-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-18
  • 1970-01-01
相关资源
最近更新 更多