Mysql查询,每行有多个子查询

时间:2013-09-18 12:53:59

标签: mysql sql

我有两个表需要从中提取数据。第一个表是应用程序,第二个表是verification_status。每个应用程序有20个步骤,verify_status包含该步骤完成的日期。我试图从一些具体步骤拉出应用程序以及完成日期。

基本上我想要的是包含所有应用程序数据的行,以及基于applications_id和verification_step_id匹配的特定验证状态行的日期。

我试过了这个,我收到一条错误,说“'where子句'中的”未知列'applications.id'“

当我取出子查询时,它工作正常。

SELECT * FROM applications,
(SELECT date FROM verification_status WHERE application_id = applications.id AND verification_step_id = 1) as steponedate
LEFT JOIN application_details ON application_details.application_id = applications.id
WHERE application_details.application_status_id != 1 AND application_details.application_status_id != 2 

3 个答案:

答案 0 :(得分:1)

放手一搏:

SELECT
  a.*,
  ad.*,
  vs.date as steponedate
FROM applications a
  LEFT JOIN verification_status vs
    ON vs.application_id = a.id
    AND vs.verification_step_id = 1
  LEFT JOIN application_details ad
    ON ad.application_id = a.id
WHERE ad.application_status_id != 1
  AND ad.application_status_id != 2;

答案 1 :(得分:0)

我不确定这是问题所在,但您似乎在查询中混合了ANSI89和ANSI92语法。我从来没有写过将两者混合在一起的工作查询。此外,似乎您运行的子查询应该针对每行数据运行,而不是作为表运行。

我认为以下查询可能适合您:

SELECT 
    applications.*, 
    (
        SELECT 
            date 
        FROM 
            verification_status 
        WHERE 
            application_id = applications.id 
            AND verification_step_id = 1
    ) as steponedate
FROM 
    applications,
    LEFT JOIN 
        application_details 
            ON application_details.application_id = applications.id
WHERE 
    application_details.application_status_id != 1 
    AND application_details.application_status_id != 2 

答案 2 :(得分:0)

尝试在此子请求中声明应用程序表:

SELECT date FROM verification_status,applications WHERE application_id = applications.id AND verification_step_id = 1