选择一个组,其中一个是,例如5 分支。因此,总位数为6。在每个6中,搜索充当3job_types LIKE "%C%" worker。如果在这些6位置之一中,是具有给定参数的3 worker ,则查询必须获取所有这些6位置的结果。

要说明: 3工作人员必须在相同的主/分支中工作。

因为项目本身非常困难,所以最好使用 RAW 查询来获得结果:

业务表

id     |    mainorbranch    |    name
--------------------------------------
1           Main                 Apple
2           Branch               Apple London
3           Branch               Apple Manchester
4           Main                 IBM
5           Branch               IBM London
etc ...

关系

业务_分支表
b_id     |    branch_id    |    id
--------------------------------------
1             1                 1
2             2                 1
3             3                 1
4             4                 4
5             5                 4
// etc

people_details表
d_id     |    id    |    job_types
--------------------------------------
1             1          C
2             3          D
3             2          F
4             4          C
5             5          C
// etc

人_支行表
pb_id     |    branch_id    |    id
--------------------------------------
1              1                 3
2              3                 2
3              4                 4
4              2                 5
5              1                 1
// etc

我需要得到什么:
Business id    |    Name    |    Postcode
-----------------------------------------
1                   Apple        postcode
2                   Apple 232    postcode
3                   Apple 323    postcode
// etc...

帮助程序的数据库结构
http://sqlfiddle.com/#!9/206733

Simplified, minified SQL file with total of 110k+ rows

更新

@KikiTheOne的回答有点奏效,但只得到一半的结果。另一半失踪了。

最佳答案

如聊天中所述。这是一个解决方案:

如果您需要公司信息...像postcode一样获取@ t1.XXXX。

我变了

"pb_id" "branch_id" "id"
"1" "1" "3"
"2" "3" "2"
"3" "1" "4"
"4" "1" "5"
"5" "1" "1"

所以我在1个分支中得到3个人
SELECT
    t1.id as "Business id",
    t1.name as Name,
    'postcode' as "Postcode"
FROM SO_business as t1 inner join
(
    SELECT * FROM SO_busness_branches as t3
    inner join
    (
        SELECT
            t5.branch_id as inner_branch,
            count(t5.branch_id) as workers_in,
            max(t6.job_types) as job_types,
            max(t7.id) as mainbranch
        FROM
            SO_people_branches as t5
                inner join SO_people_details as t6
                    on t5.id = t6.id
                inner join SO_busness_branches as t7
                    on t5.branch_id = t7.branch_id
        WHERE
            t6.job_types LIKE '%C%'
        GROUP BY
            t5.branch_id
    ) as t4
        on t3.id = t4.inner_branch
    WHERE t4.workers_in >= 3
) as t2
    on t1.id = t2.branch_id

解释:

-.1最内部的SQL计数带有worker(worker数量init)和Job_type =%c%的ALL分支,并加入分支的MAIN ID。

-.2第二个SQL获取该信息,并且仅选择worker> = 3的所有分支

-.3外部SQL选择所有内部INFOS,并从内部SQL中返回带有branchID-Main的所有分支/主体。并将它们连接到业务表,这样您就可以从那里显示所有信息,例如邮政编码

10-08 09:02
查看更多