简体   繁体   English

如何基于SQL Server中当前表列的值使用与其他表不同的列值

[英]How to use different column values from other table based on value on current table column in SQL server

I have a table A with [agent name] and [conducted by] columns 我有一个带有[代理名称]和[进行者]列的表A

在此处输入图片说明

I have an another table B with [Agent Names],[Manager] and [SDI] columns 我还有另一个表B,其中包含[代理商名称],[经理]和[SDI]列

在此处输入图片说明

I need a result like the table below 我需要类似下表的结果

在此处输入图片说明

result table is based on table A and Table B .If the [Conducted by] field value in Table A is 0 then i need to get [manager] column value from table B,else if my [conducted by] column value in table A is 1 then i need to get [SDI] column value from table B.how can i achieve this? 结果表基于表A和表B。如果表A中的[Conducted]字段值是0,那么我需要从表B中获取[manager]列值,否则,如果我的A表中的[conducted]列值是1,那么我需要从表B中获取[SDI]列值。我如何实现这一点?

Thanks in adavance 提前感谢

Try this: 尝试这个:

SELECT
  a.agentName,
  CASE
    WHEN a.conductedBy=0 THEN b.manager
    ELSE b.sdi
  END AS conductedBy
FROM tableA AS a
JOIN tableB AS b ON a.agentName=b.agentName

or this: 或这个:

SELECT
  a.agentName,
  CASE a.conductedBy
    WHEN 0 THEN b.manager
    ELSE b.sdi
  END AS conductedBy
FROM tableA AS a
JOIN tableB AS b ON a.agentName=b.agentName

only for mysql work this: 仅用于mysql工作:

SELECT
  a.agentName,
  IF(a.conductedBy=0, b.manager, b.sdi) AS conductedBy
FROM tableA AS a
JOIN tableB AS b ON a.agentName=b.agentName

您可以使用case语句:

case when ConductedBy = 0 then [Manager] when ConductedBy = 1 then [SDI] end

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

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