简体   繁体   中英

MySQL join inside subquery

I have this:

# tab1 = Company
# tab2 = Forms
# tab3 = Filled forms from each company enabled to it.

# Search Companies with pending/empty Forms.
# Company #1 has 3 forms, all filled, not will be listed;
# Company #2 has form 2 and 3, only form 2 is filled, so it will be listed;
# Company #3 has not form filled, so it will be listed;

SELECT company.*
FROM tab1 as company

WHERE (
  SELECT TRUE
  FROM tab2 AS forms

  # Join compatible company forms results/fills.
  # Itll join with company.id (that here not exists) and the forms.id (that exists)
  LEFT JOIN tab3 AS fills ON
    fills.id_tab1 = company.id AND
    fills.id_tab2 = forms.id

  # Itll filter forms that company have.
  # And will accept ONLY if none result is found, what mean "pending fill" on company.
  # I need just that.
  WHERE 
    FIND_IN_SET(forms.id, company.id_forms) AND
    fills.id IS NULL
)

And I get Unknown column tab1.id .
How to solve it?

I need have access to tab1 columns inside of INNER of SUBQUERY.

Update #1 - Live sample : http://sqlfiddle.com/#!2/20aa5/3

try this code, the logic is the same

 SELECT tab1.* from tab1
        inner join tab3 ON  tab3.id_tab1 = tab1.id
    WHERE .....

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