子查询内的MySQL连接
我有这个:子查询内的MySQL连接,mysql,inner-join,Mysql,Inner Join,我有这个: # 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,
# 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
)
我得到未知列tab1.id
如何解决 我需要访问子查询内部的
tab1
列
更新#1-
实时示例:试试这段代码,逻辑是一样的
SELECT tab1.* from tab1
inner join tab3 ON tab3.id_tab1 = tab1.id
WHERE .....
你到底想做什么?考虑提供适当的DDL(和/或SqLFIDLE)和期望的结果集,而不是只说“试试这个”,解释它是如何解决这个问题的(如果它确实是这样,我认为它没有)。我只是尽我所能去解决这个问题,如果它不起作用的话,David Rodrigues会尝试其他的东西。如果您看到他的原始代码tab.2与逻辑完全不相关,那么如果tab1.id=tab3.id,那么在tab2上选择true就是多余的。