简体   繁体   中英

mysql using outside alias to 2 level deep subquery

I'm currently stuck here and don't know what to do next since I can't use the tv_main alias into the 2 level deep subquery. Here's my code (I commented the part that have a problem). Any help will be appreciated. Thanks.

SELECT tv_main.id,
    tv_main.vesselName,
    (
        SELECT  SUM(t_statCtr.status = 'EX CREW')
        FROM
        (
                (
                    SELECT  tpi_stat.id,
                            tvv.vesselName,
                            lastname, 
                            firstname, 
                            middlename, 
                            IF(tpi_stat.returningCrew = 1, 'NEW HIRE', 
                                IF(COUNT(tc_ctr.personnel_id) > 1, 'EX CREW', 'NEW HIRE')
                              ) 
                            AS status
                    FROM tbl_contracts AS tc_stat
                    LEFT JOIN tbl_personnel_info AS tpi_stat
                    ON tpi_stat.id = tc_stat.personnel_id 

                    LEFT JOIN tbl_contracts AS tc_ctr
                    ON tpi_stat.id = tc_ctr.personnel_id

                    LEFT JOIN tbl_vessels AS tvv
                    ON tvv.id = tpi_stat.lastJoinedVsl

                    WHERE
                            tpi_stat.emp_status = 'ON-BOARD'
                            AND tc_stat.status = 'ACTIVE'
                            AND tvv.id = tv_main.id --This line have an error, (Unknown Column tv_main.id in where clause)
                            GROUP BY tc_stat.personnel_id
                ) AS t_statCtr
        )
    ) AS ex_crew,
    NULL AS new_hire
    FROM tbl_vessels AS tv_main -- I need this one to use inside the subquery
    LEFT JOIN tbl_personnel_info AS tpi
    ON tv_main.id = tpi.lastJoinedVsl

    LEFT JOIN tbl_contracts AS tc
    ON tpi.id = tc.personnel_id 
    WHERE
    tpi_stat.emp_status = 'ON-BOARD'
    AND tc_stat.status = 'ACTIVE'
    GROUP BY tv_main.vesselName

I finally solve it. I didn't know that mysql only allow correlation of 1 deep level.

SELECT  tv_main.vesselName,
    SUM(t_dummy.statusx = 'EX CREW') AS ex_crew,
    SUM(t_dummy.statusx = 'NEW HIRE') AS new_hire
FROM tbl_vessels AS tv_main

LEFT JOIN tbl_personnel_info AS tpi_main 
ON tpi_main.lastJoinedVsl = tv_main.id

LEFT JOIN tbl_contracts AS tc_main
ON tc_main.personnel_id = tpi_main.id

 LEFT JOIN
(
    SELECT  tvv.id,
            tpi_stat.id AS tpiid,
            IF(tpi_stat.returningCrew = 1, 'NEW HIRE', 
                IF(COUNT(tc_ctr.personnel_id) > 1, 'EX CREW', 'NEW HIRE')
                ) 
            AS statusx
    FROM tbl_contracts AS tc_stat
    LEFT JOIN tbl_personnel_info AS tpi_stat
    ON tpi_stat.id = tc_stat.personnel_id 

    LEFT JOIN tbl_contracts AS tc_ctr
    ON tpi_stat.id = tc_ctr.personnel_id

    LEFT JOIN tbl_vessels AS tvv
    ON tvv.id = tpi_stat.lastJoinedVsl
    GROUP BY tc_stat.personnel_id  
) AS t_dummy
ON tpi_main.id = t_dummy.tpiid 
WHERE
tpi_main.emp_status = 'ON-BOARD'
AND tc_main.status = 'ACTIVE'
AND t_dummy.id = tv_main.id
GROUP BY tv_main.vesselName;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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