SELECT IFNULL(oea.`name`,op.name) AS "Consultation"
FROM `a` ca
JOIN `b` oea ON ca.`consultation_id` = oea.id AND oea.status = 1
LEFT OUTER JOIN c oeal ON oea.`location_id` = oeal.id
WHERE oea.employee_profile_id IN (
SELECT profile_id
FROM d pp
WHERE pp.status='1'
UNION
SELECT op.id
FROM e op
WHERE op.status
) AND
ca.status = 1
我收到此错误
Unknown column 'op.name' in 'field list'
是否有可能在范围之外使用op.name?
答案 0 :(得分:2)
JOIN
WHERE
子句中的子查询,而不是使用IN
谓词。这样您就可以从中选择名称。
但是你没有从子查询中选择列name
。您只选择了profile_id
,您必须使用profile_id
选择它。类似的东西:
SELECT
IFNULL(oea.`name`, op.name) AS Consultation
FROM `a` ca
JOIN `b` oea ON ca.`consultation_id` = oea.id AND oea.status = 1
INNER JOIN
(
SELECT profile_id, name
FROM d pp
WHERE pp.status='1'
UNION
SELECT op.id, name
FROM e op
WHERE op.status
) AS op ON oea.employee_profile_id = op.profile_id
LEFT OUTER JOIN c oeal ON oea.`location_id` = oeal.id
WHERE ca.status = 1;
我认为这两个表都有name
列,如果其中一个没有,则必须使用null
。