我有一张表A,其中包含[代理商名称]和[由[进行]列
我有另一个表B,其中包含[代理商名称],[经理]和[SDI]列
我需要一个如下表所示的结果
结果表基于表A和表B.如果表A中的[Conducted by]字段值为0,那么我需要从表B中获取[manager]列值,否则如果我的[由...导入]列值在表A中是1然后我需要从表B得到[SDI]列值。我可以实现这个吗?
谢谢你
答案 0 :(得分:2)
试试这个:
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
或者这个:
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
只为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
答案 1 :(得分:1)
您可以使用case
声明:
case when ConductedBy = 0 then [Manager] when ConductedBy = 1 then [SDI] end