MySQL查询中的未知列

时间:2017-11-01 17:14:01

标签: mysql

我有以下查询

SET @CompanyId = "2030";

SELECT DISTINCT (NAME) 
FROM i18ninterviewmodel 
WHERE LocaleId='1' 
AND InterviewModelId IN (
    SELECT Id 
    FROM interviewmodel 
    WHERE TYPE IN ('ABC','123') 
    AND Id IN (
        SELECT InterviewModelId 
        FROM companyinterview 
        WHERE STATUS='ACTV' 
        AND CompanyId=@CompanyId
    )
)
ORDER BY NAME ASC;

我想通过使用JOINS来重写它,但是我遇到了问题。当我使用下面的内容时,我不断收到错误消息

  

'where子句'中的未知列'companyinterview.Status'

SET @CompanyId = "2030";

SELECT i18ninterviewmodel.Name 
FROM i18ninterviewmodel
JOIN i18ninterviewmodel i18 
    ON interviewmodel.Id = i18.InterviewModelId
JOIN interviewmodel 
    ON companyinterview.InterviewModelId = interviewmodel.Id
WHERE i18ninterviewmodel.LocaleId='1' 
AND companyinterview.Status='ACTV' 
AND companyinterview.CompanyId=@CompanyId 
AND interviewmodel.type IN ('ABC','123') 
ORDER BY i18ninterviewmodel.Name ASC;

我对MySQL没有太多经验,所以我不确定问题出在哪里。

2 个答案:

答案 0 :(得分:0)

我在任何时候都看不到你加入表companyinterview的位置。

我想你想写这个:

SET @CompanyId = "2030";
SELECT i18ninterviewmodel.Name FROM i18ninterviewmodel
JOIN i18ninterviewmodel i18 ON interviewmodel.Id = i18.InterviewModelId
JOIN companyinterview ON companyinterview.InterviewModelId = interviewmodel.Id
WHERE i18ninterviewmodel.LocaleId='1' AND companyinterview.Status='ACTV' AND companyinterview.CompanyId=@CompanyId AND interviewmodel.type IN ('ABC','123') ORDER BY i18ninterviewmodel.Name AS

答案 1 :(得分:0)

从联接中删除,然后添加到

   SET @CompanyId = "2030";
        SELECT i18ninterviewmodel.Name FROM i18ninterviewmodel
        JOIN i18ninterviewmodel i18 ON interviewmodel.Id = i18.InterviewModelId
        JOIN interviewmodel ON interviewmodel.Id
        WHERE i18ninterviewmodel.LocaleId='1' AND companyinterview.Status='ACTV' AND companyinterview.CompanyId=@CompanyId
AND interviewmodel.type IN ('ABC','123') 
AND  companyinterview.InterviewModelId =i18.InterviewModelId
ORDER BY i18ninterviewmodel.Name ASC