Select the group in which the Primary is located, and, for example, 5 Branch . So, the total number of seats is 6 . In each of these 6 find 3 workers who work as job_types LIKE "%C%" . If in one of these 6 places there are 3 workers with a given parameter, the query should get the results of all these 6 places.
To clarify: 3 workers should work in one main / branch.
Since the project itself is very dense, it would be better to get the results using the RAW query:
business table
id | mainorbranch | name -------------------------------------- 1 Main Apple 2 Branch Apple London 3 Branch Apple Manchester 4 Main IBM 5 Branch IBM London etc ...
Relations
business_branches table
b_id | branch_id | id -------------------------------------- 1 1 1 2 2 1 3 3 1 4 4 4 5 5 4
people_details table
d_id | id | job_types -------------------------------------- 1 1 C 2 3 D 3 2 F 4 4 C 5 5 C
people_branches table
pb_id | branch_id | id -------------------------------------- 1 1 3 2 3 2 3 4 4 4 2 5 5 1 1
What I need to get:
Business id | Name | Postcode ----------------------------------------- 1 Apple postcode 2 Apple 232 postcode 3 Apple 323 postcode
DB structure for helpers http://sqlfiddle.com/#!9/206733
Simplified, reduced SQL file with 110k + total rows
UPDATE
@KikiTheOne's answer to this question, but it only gets half the result. The other half is missing.
source share