I need help writing a complex exclusion sql query (at least for me). First I am describing about requirement and queries here…
Say
-
I have a table “my_users” where user’s name and user_ids are maintained.
-
I have a table “my_skills” where I maintain a skill name and corresponding skill_id.
-
I have a table “my_users_skills” where users and their corresponding skill_ids are maintained.
-
I have a table “my_companies” where I maintain a company name and corresponding company_ids.
-
I have a table “my_employer_profile” that contians employer-users id, and employer-company-id
-
I have a table “my_users_blocked_companies” , it contains users ids and corresponding blocked company ids.
Purpose of “my_users_blocked_companies” table, is to maintain 1 or more companies for users as blocked companies.
When HR person of one company search for users, users who has set that company (where HR is working) as a blocked company should not come in search result.
Lets get to SQL queries
1.
DROP TABLE IF EXISTS my_users;
CREATE TABLE my_users
(
user_id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
name varchar(20) NOT NULL UNIQUE,
age int NULL
)
INSERT INTO my_users VALUES
(1, "John", 20),
(2, "Kelly", 25),
(3, "Xavier", 50),
(4, "Krishna", 40);
2.
DROP TABLE IF EXISTS my_skills;
CREATE TABLE my_skills
(
skill_id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
skill varchar(20) NOT NULL UNIQUE
);
INSERT INTO my_skills VALUES
(1, "MySql"),
(2, "Php"),
(3, "Linux"),
(4, "Java");
3.
DROP TABLE IF EXISTS my_users_skills;
CREATE TABLE my_users_skills
(
user_id int NOT NULL,
skill_id int NOT NULL
);
INSERT INTO my_users_skills VALUES
(1,2),
(1,4),
(2,1),
(2,4),
(3,2),
(3,3);
4.
DROP TABLE IF EXISTS my_companies;
CREATE TABLE my_companies
(
company_id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
company varchar(20) NOT NULL UNIQUE
);
INSERT INTO my_companies VALUES
(1, "Motorola"),
(2, "Logica"),
(3, "Microsft"),
(4, "Texas Instruements");
5.
DROP TABLE IF EXISTS my_employer_profile;
CREATE TABLE my_employer_profile
(
user_id int NOT NULL,
comp_id int NOT NULL
);
INSERT INTO my_employer_profile VALUES
(4,1),
Now it is clear that “Krishna” is an employee in company Motorola.
6.
DROP TABLE IF EXISTS my_users_blocked_companies;
CREATE TABLE my_users_blocked_companies
(
user_id int NOT NULL,
comp_id int NOT NULL
);
INSERT INTO my_users_blocked_companies VALUES
(1,1),
(1,4),
(2,4)
Now you can clearly see John has set Motorola and Texas Instruements as blocked companies.
Also kelly has set Texas Instruements as blocked company.
Now if Krishna is searching for users who has skill set ‘java’,’mysql’, we can have two possible cases:
Case 1
The search result, without considering blocked companies will fetch records for John and Kelly.
The SQL query for this case is
SELECT x.name
, GROUP_CONCAT(DISTINCT y2.skill ORDER BY y2.skill) skills
FROM my_users x
JOIN my_users_skills xy1
ON xy1.user_id = x.user_id
JOIN my_skills y1
ON y1.skill_id = xy1.skill_id
AND y1.skill IN ('java','mysql')
JOIN my_users_skills xy2
ON xy2.user_id = xy1.user_id
JOIN my_skills y2
ON y2.skill_id = xy2.skill_id
GROUP
BY x.user_id;
Case 2
And the search result, with considering blocked companies will fetch records for Kelly as John has set Motorola as a blocked company, and Krishna is from Motorola
For this case, I have struggled a lot, but not able to write the right query.
You can do it like this:
sqlfiddle execution here